[英]Php preg_replace() using
我正在尝试更改字符串的某些部分。 我为此使用了preg_replace()函数,但无法成功。
这是一个例子。
$str = "Suspendisse rutrum rhoncus leo vitae vehicula. <span class="sdsad"> Nunc nec dapibus nisi.</span> Donec facilisis mauris sapien, eget blandit enim dignissim auctor. <span style="text-decoration: underline;" class="sadsad">Nullam a porta orci.</span>";
我需要让零件以"<span"
开头,直到">"
字符并将其转为<p>
或其他名称。
$str = preg_replace('/<span.*>/', '<p>', $str);
我正在试图解决这样的问题,但它返回相同的值。
我需要做什么?
谢谢..
此正则表达式将为您解决问题。
$str = 'Suspendisse rutrum rhoncus leo </span> vitae vehicula. <span class="sdsad"> Nunc nec dapibus nisi.</span> Donec facilisis mauris sapien, eget blandit enim dignissim auctor. <span style="text-decoration: underline;" class="sadsad">Nullam a porta orci.</span>';
$str_replaced = preg_replace('/<(\/{0,1})span[^>]*>/','<$1p>',$str);
echo $str_replaced;
可以选择将尾部斜杠放入标记中,因此您只需要一个调用。
您在这里忘记了捕获小组()
$str = preg_replace('/<span(.*)>/', '<p>', $str);
此处提供更多信息: http : //www.regular-expressions.info/brackets.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.