用符号或符号替换附加 html 标记值

发布于 2024-12-21 03:15:37 字数 950 浏览 0 评论 0原文

<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>
<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. 

using php reg expression or DOMDocument() i need to replace the all the content to be appended with $ symbol.

For ex:

<span class="fwb">Harry and David</span> to be replaced by <span class="fwb">Harry and David
lt;/span>

<span class="fcg">Business Intelligence Developer/Analyst</span> to be replaced by       <span class="fcg">Business Intelligence Developer/Analyst
lt;/span>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

庆幸我还是我 2024-12-28 03:15:37

从您的代码示例中很难理解(?),但我相信您希望在每个 标记的末尾附加一个 $ 符号。使用 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 . '

您可以,正如你所说,也使用 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 节点的末尾。

; echo $dom->saveHTML($span) . "\n"; }

您可以,正如你所说,也使用 preg_replace() 像这样:

此正则表达式示例将 $ 附加到主题 HTML 文本中每个 span 节点的末尾。

It's difficult to understand from your code samples (?) but I believe you're looking to append a $ sign to the end of each of the <span> tags. Using DomDocument to do this:

$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 . '

You could, as you said, also use preg_replace() like so:

$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);

This regex example appends $ to the end of each span node in the subject HTML text.

; echo $dom->saveHTML($span) . "\n"; }

You could, as you said, also use preg_replace() like so:

This regex example appends $ to the end of each span node in the subject HTML text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文