我们正在做一个项目,在这个项目中,我们必须使用 PHP 将 html 数据替换为数组值preg_replace()
请看代码
代码
$html = new simple_html_dom();
$texts = array('Replace','the', 'asterisks','prefer','it','all','functions','will','replace','computer','strategic','casio','computing','smart');
$html->load("<body><div>Replace the asterisks in a list with numbers in order</div><div>or if you prefer it all condensed down into a single smart</div><li>Both functions will replace placeholder</li><li>smart</li></body>");
$sample = &$html->outertext ;
foreach($texts as $text){
$fine = trim($text);
$replace = '<u>'.$fine.'<\u>';
if(!empty($text)){
if (strpos($sample,$fine)){
$sample = preg_replace('/$fine/',$replace,$sample);
$html->save(); /// Update all the replaces on $html ;
}
}
}
echo $sample;
打印相同$html
,未更新。