I'm trying to change some parts of a string. I used preg_replace() function for this stuation but I couldn't succeed.
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.
$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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.