<span class="fwb">Harry and David</span>
<span class="fcg">Business Intelligence Developer/Analyst</span>
<span class="fcg">Oct 1998 to present</span>
<span class="fcg">Medford, Oregon</span>
<span class="fsm fwn fcg">Creative writing</span>
In my html content
<span class="fwb"> occured more than 4 times,
<span class="fcg"> occured more than 10 times,
<span class="fsm fwn fcg">Creative writing</span> occured more than 5 times.
使用php reg表达式或DOMDocument()我需要替换要附加$符号的所有内容。
例如:
<span class="fwb">Harry and David</span> to be replaced by <span class="fwb">Harry and David$</span>
<span class="fcg">Business Intelligence Developer/Analyst</span> to be replaced by <span class="fcg">Business Intelligence Developer/Analyst$</span>
答案 0 :(得分:0)
从您的代码示例(?)中很难理解,但我相信您希望在每个$
标记的末尾添加<span>
符号。使用DomDocument
执行此操作:
$str = '
<span class="fwb">Harry and David</span>
<span class="fcg">Business Intelligence Developer/Analyst</span>
';
$dom = new DomDocument();
$dom->loadHTML($str);
$spans = $dom->getElementsByTagName('span');
foreach ($spans as $span) {
$span->nodeValue = $span->nodeValue . '$';
echo $dom->saveHTML($span) . "\n";
}
正如你所说,你可以像这样使用preg_replace()
:
$str = '
<span class="fwb">Harry and David</span>
<span class="fcg">Business Intelligence Developer/Analyst</span>
';
$p = '{(<span[^>]*>)([^<]+)(</span>)}';
$r = '$1$2\$$3';
echo preg_replace($p, $r, $str);
此正则表达式示例将$
附加到主题HTML文本中每个span节点的末尾。