<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(),我需要用$ symbol替换所有的内容。
为例:<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>
很难从您的代码示例(?)中理解,但我相信您希望在每个<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);
这个regex示例将$
附加到主题HTML文本中每个span节点的末尾。