[英]Php preg_replace() using
I'm trying to change some parts of a string. 我正在尝试更改字符串的某些部分。 I used preg_replace() function for this stuation but I couldn't succeed.
我为此使用了preg_replace()函数,但无法成功。
Here is an example. 这是一个例子。
$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>";
I need to get parts begin with "<span"
and after all till ">"
charecter and turn it <p>
or something else. 我需要让零件以
"<span"
开头,直到">"
字符并将其转为<p>
或其他名称。
$str = preg_replace('/<span.*>/', '<p>', $str);
I'm trying to solve this like that but it returns the same value. 我正在试图解决这样的问题,但它返回相同的值。
What do I need to do this ? 我需要做什么?
Thank you.. 谢谢..
This Regex will do the trick for you. 此正则表达式将为您解决问题。
$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;
It will optionally put the trailing slash into the tags so you will only need one call. 可以选择将尾部斜杠放入标记中,因此您只需要一个调用。
You forgot the capturing group here ()
您在这里忘记了捕获小组
()
$str = preg_replace('/<span(.*)>/', '<p>', $str);
More info here: http://www.regular-expressions.info/brackets.html 此处提供更多信息: http : //www.regular-expressions.info/brackets.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.