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

时间:2011-10-18 12:31:29

标签: php regex preg-replace

我遇到以下问题而且我的正则表达式并不是很好,所以请有人帮帮我吗?

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

我需要在每个锚标记内的image标记之后替换纯文本字符串。字符串始终是0到99之间的数字。锚标记的ID也是自动生成的,也是title属性。我不知道如何只获得所有标签上的数字。我需要用空字符串''替换它。有人可以帮助我并解释我怎么能这样做吗?

非常感谢!!

3 个答案:

答案 0 :(得分:1)

尝试

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

这将替换图像标记后的图像标记+文本,仅包含图像标记。

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

preg_replace('|>[0-9]{1,2}<|','><',$html);