PHP:preg_replace() - 需要替换textNode字符串

发布于 2024-12-10 16:23:30 字数 1737 浏览 1 评论 0原文

我遇到以下问题,并且我不太擅长正则表达式,所以请问有人可以帮助我吗?

<div class="bulletPoints">
    <div>
        <a href="#autoGeneratedId0" title="test_f01"><img src="../images/test_f01.jpg" alt="test_f01"/>1</a>
        <a href="#autoGeneratedId1" title="test_f02"><img src="../images/test_f02.jpg" alt="test_f02"/>2</a>
        <a href="#autoGeneratedId2" title="test_f03"><img src="../images/test_f03.jpg" alt="test_f03"/>3</a>
        <a href="#autoGeneratedId3" title="test_f04"><img src="../images/test_f04.jpg" alt="test_f04"/>4</a>
        <a href="#autoGeneratedId4" title="test_f05"><img src="../images/test_f05.jpg" alt="test_f05"/>5</a>
        <a href="#autoGeneratedId5" title="test_f06"><img src="../images/test_f06.jpg" alt="test_f06"/>6</a>
        <a href="#autoGeneratedId6" title="test_f07"><img src="../images/test_f07.jpg" alt="test_f07"/>7</a>
        <a href="#autoGeneratedId7" title="test_f08"><img src="../images/test_f08.jpg" alt="test_f08"/>8</a>
        <a href="#autoGeneratedId8" title="test_f09"><img src="../images/test_f09.jpg" alt="test_f09"/>9</a>
        <a href="#autoGeneratedId9" title="test_f09"><img src="../images/test_f09.jpg" alt="test_f09"/>10</a>
        <a href="#autoGeneratedId10" title="test_f09"><img src="../images/test_f09.jpg" alt="test_f09"/>11</a>
        <!-- and so on -->
    </div>
</div>

我需要替换每个锚标记内的图像标记后面的纯文本字符串。该字符串始终是 0 - 99 之间的数字。锚标记的 ID 和标题属性也是自动生成的。我不知道如何只获取所有标签上的数字。我需要将其替换为空字符串 ''。有人可以帮助我并解释我该怎么做吗?

非常非常感谢!!

I have the following problem and I am not really good in regular expressions, so please, could somebody help me out?

<div class="bulletPoints">
    <div>
        <a href="#autoGeneratedId0" title="test_f01"><img src="../images/test_f01.jpg" alt="test_f01"/>1</a>
        <a href="#autoGeneratedId1" title="test_f02"><img src="../images/test_f02.jpg" alt="test_f02"/>2</a>
        <a href="#autoGeneratedId2" title="test_f03"><img src="../images/test_f03.jpg" alt="test_f03"/>3</a>
        <a href="#autoGeneratedId3" title="test_f04"><img src="../images/test_f04.jpg" alt="test_f04"/>4</a>
        <a href="#autoGeneratedId4" title="test_f05"><img src="../images/test_f05.jpg" alt="test_f05"/>5</a>
        <a href="#autoGeneratedId5" title="test_f06"><img src="../images/test_f06.jpg" alt="test_f06"/>6</a>
        <a href="#autoGeneratedId6" title="test_f07"><img src="../images/test_f07.jpg" alt="test_f07"/>7</a>
        <a href="#autoGeneratedId7" title="test_f08"><img src="../images/test_f08.jpg" alt="test_f08"/>8</a>
        <a href="#autoGeneratedId8" title="test_f09"><img src="../images/test_f09.jpg" alt="test_f09"/>9</a>
        <a href="#autoGeneratedId9" title="test_f09"><img src="../images/test_f09.jpg" alt="test_f09"/>10</a>
        <a href="#autoGeneratedId10" title="test_f09"><img src="../images/test_f09.jpg" alt="test_f09"/>11</a>
        <!-- and so on -->
    </div>
</div>

I need to replace the plain text string after the image tag inside of each anchor tag. The string is always a number from 0 - 99. The IDs of the anchor tags are auto generated and the title attribute, too. I don't know how to reach only the number on all tags. I need to replace it with an empty string ''. Could somebody help me and explain how I can do that??

Thank you very very much!!

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

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

发布评论

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

评论(3

雨后咖啡店 2024-12-17 16:23:30

尝试

preg_replace('/(<img[^>]+>)(\d+)/', "\\1", $text);

此操作将仅用图像标签替换图像标签+图像标签后的文本。

Try

preg_replace('/(<img[^>]+>)(\d+)/', "\\1", $text);

This will replace the image tag + text after the image tag with only the image tag.

飘逸的'云 2024-12-17 16:23:30

您可以将 />[0-9]{,2}< 替换为 /><

test with sed:

echo 'yourtext'|sed -r 's#/>[0-9]{,2}<#/><#g'

输出:

<div class="bulletPoints">
    <div>
        <a href="#autoGeneratedId0" title="test_f01"><img src="../images/test_f01.jpg" alt="test_f01"/></a>
        <a href="#autoGeneratedId1" title="test_f02"><img src="../images/test_f02.jpg" alt="test_f02"/></a>
        <a href="#autoGeneratedId2" title="test_f03"><img src="../images/test_f03.jpg" alt="test_f03"/></a>
        <a href="#autoGeneratedId3" title="test_f04"><img src="../images/test_f04.jpg" alt="test_f04"/></a>
        <a href="#autoGeneratedId4" title="test_f05"><img src="../images/test_f05.jpg" alt="test_f05"/></a>
        <a href="#autoGeneratedId5" title="test_f06"><img src="../images/test_f06.jpg" alt="test_f06"/></a>
        <a href="#autoGeneratedId6" title="test_f07"><img src="../images/test_f07.jpg" alt="test_f07"/></a>
        <a href="#autoGeneratedId7" title="test_f08"><img src="../images/test_f08.jpg" alt="test_f08"/></a>
        <a href="#autoGeneratedId8" title="test_f09"><img src="../images/test_f09.jpg" alt="test_f09"/></a>
        <a href="#autoGeneratedId9" title="test_f09"><img src="../images/test_f09.jpg" alt="test_f09"/></a>
        <a href="#autoGeneratedId10" title="test_f09"><img src="../images/test_f09.jpg" alt="test_f09"/></a>
        <!-- and so on -->
    </div>
</div>

you can replace />[0-9]{,2}< with /><

test with sed:

echo 'yourtext'|sed -r 's#/>[0-9]{,2}<#/><#g'

output:

<div class="bulletPoints">
    <div>
        <a href="#autoGeneratedId0" title="test_f01"><img src="../images/test_f01.jpg" alt="test_f01"/></a>
        <a href="#autoGeneratedId1" title="test_f02"><img src="../images/test_f02.jpg" alt="test_f02"/></a>
        <a href="#autoGeneratedId2" title="test_f03"><img src="../images/test_f03.jpg" alt="test_f03"/></a>
        <a href="#autoGeneratedId3" title="test_f04"><img src="../images/test_f04.jpg" alt="test_f04"/></a>
        <a href="#autoGeneratedId4" title="test_f05"><img src="../images/test_f05.jpg" alt="test_f05"/></a>
        <a href="#autoGeneratedId5" title="test_f06"><img src="../images/test_f06.jpg" alt="test_f06"/></a>
        <a href="#autoGeneratedId6" title="test_f07"><img src="../images/test_f07.jpg" alt="test_f07"/></a>
        <a href="#autoGeneratedId7" title="test_f08"><img src="../images/test_f08.jpg" alt="test_f08"/></a>
        <a href="#autoGeneratedId8" title="test_f09"><img src="../images/test_f09.jpg" alt="test_f09"/></a>
        <a href="#autoGeneratedId9" title="test_f09"><img src="../images/test_f09.jpg" alt="test_f09"/></a>
        <a href="#autoGeneratedId10" title="test_f09"><img src="../images/test_f09.jpg" alt="test_f09"/></a>
        <!-- and so on -->
    </div>
</div>
情泪▽动烟 2024-12-17 16:23:30
preg_replace('|>[0-9]{1,2}<|','><',$html);
preg_replace('|>[0-9]{1,2}<|','><',$html);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文