且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用于替换和添加属性到 HTML 标签的 RegEx

更新时间:2023-02-21 20:05:09

我认为***的方法是使用 preg_replace_callback.

I think the best approach is to use preg_replace_callback.

此外,我建议使用比目前所建议的更严格的 regexp - 如果您的页面包含一个 <img/> 标记 包含id属性?

Also I would recommend a slightly more stringent regexp than those suggested so far - what if your page contains an <img /> tag that does not contain an id attribute?

$page = '
<body>
  <img src="source.jpg" />
  <p>
    <img src="source.jpg" id ="hello" alt="nothing" />
    <img src="source.jpg" id ="world"/>
  </p>
</body>';

function my_callback($matches)
{
    static $i = 0;
    return $matches[1]."img_".$i++;
}

print preg_replace_callback('/(<img[^>]*ids*=s*")([^"]*)/', "my_callback", $page);

为我产生以下结果:

<body>
  <img src="source.jpg" />
  <p>
    <img src="source.jpg" id ="img_0" alt="nothing" />
    <img src="source.jpg" id ="img_1"/>
  </p>
</body>

regexp 有两个捕获组,第一个我们保留,第二个我们替换.我使用了很多否定字符类(例如 [^>]* = 直到关闭 >)来确保 <img/> 标签不需要具有 id 属性.

The regexp has two capturing groups, the first we preserve, the second we replace. I've used lots of negative character classes (e.g. [^>]* = up to closing >) to make sure that <img /> tags arn't required to have id attributes.