[英]Using regex in preg_replace to match an html href anchor tag
[英]Using preg_replace to match html tag style-attributes
我有以下正则表达式:
preg_replace('#<(/?(?:pre|p|b|em|u|ul|li|ol|blockquote|h1|h2|h3|h4|h5|strong|br))>#', '<\1>', $this->body);
它可以很好地替换没有样式属性的标签,例如:
<p>, <b>, <li>
但是我也试图使其与具有样式属性的标签匹配,例如:
<p style="margin-left: 20px"> and <p style="text-align: right;">.
我怎样才能做到这一点?
谢谢你的帮助!
尝试这个:
<(/?(?:pre|p|b|em|u|ul|li|ol|blockquote|h1|h2|h3|h4|h5|strong|br))(?: +[a-z]+="[^"]*")*>
最后一部分:
(?: +[a-z]+="[^"]*")*
匹配attribute =“ xxxxx”
表单注释:我只需要style属性-且仅用于
<p>
-tags!
从索引1获取匹配的组。
<p.*(style="[^"]*")[^>]*>
表单注释:除了style属性外,其他所有属性都应省略!
尝试在下面的示例代码中将p
标签仅替换为style属性。
样例代码:
$re = "/<p.*(style=\\"[^\\"]*\\")[^>]*>/m";
$str = "<p style=\"margin-left: 20px\" align=\"left\">\n<p align=\"left\">\n<p align=\"left\" style=\"margin-left: 20px\">\n";
$subst = '<p $1>';
$result = preg_replace($re, $subst, $str);
查找有关抓取HTML标签的详细说明
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.