Regex - 用指定的属性值替换HTML标记的模式

时间:2015-12-30 15:31:13

标签: php html regex replace html-parsing

我正在使用PHP,我想检查以下标记: iframe 脚本

我希望如果它们在 src 属性中包含X值,则将Y类添加到标记并将 src 属性名称更改为 data-src < / em>的

离。

$blacklist = array("google.com/plus.js", "google.com/drive.js");
$myclass = "blocked";

$html = '<script src="http://google.com/plus.js"></script>';

foreach($blacklist as $black)
{
    $html = preg_replace(...);
}

/* now $html must be: "<script data-src="google.com/plus.js" class="blocked"></script> */

任何人都可以帮我完成这个Regex操作吗?

1 个答案:

答案 0 :(得分:1)

我想帮忙。

在vim中使用正则表达式进行搜索。 enter image description here

在vim中使用正则表达式进行搜索。 enter image description here

用vim中的正则表达式替换。 enter image description here