简体   繁体   English

正则表达式-用指定属性值替换HTML标签的模式

[英]Regex - Pattern to Replace a HTML tag with specified attribute value

i'm using PHP and i want to check the following tags: iframe and script 我正在使用PHP,并且想检查以下标签: iframe脚本

I want that if they contains X value in the src attribute to add a Y class to the tag and change src attribute name to data-src . 我希望如果它们在src属性中包含X值,以将Y类添加到标签并将src属性名称更改为data-src

ex. 例如

$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> */

Anyone can help me with this Regex operation? 有人可以帮我进行此Regex操作吗?

I want to help. 我想帮忙

To search with regex in the vim. 在vim中使用正则表达式进行搜索。 在此处输入图片说明

To search with regex in the vim. 在vim中使用正则表达式进行搜索。 在此处输入图片说明

To substitute with regex in the vim. 在vim中用正则表达式代替。 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM