preg_match_all and preg_replace - regexp placeholders replacement example
Posted: Mon Oct 05, 2020 12:12 am
I liked to answer this at phpbb.com, so i would like to put this piece of code as memo (because so useful in many scenarios with little changes):
The regexp has been little improved in this version by the original answer.
Anyway, the nice to me, is not the regexp, but the way preg_match_all and preg_replace have been used to manage placeholders replacements on the string, with a foreach
Needed result:
any instance like
<https://blahblahblah>
need to be switched into
https://blahblahblah
where could exist into a string, also something like this:
<https://blahblah>blublublu>
So, into same php file (it is divided to make it more clear):
See resulting html source.
Do you know if there is a faster and shorter version of php code, to achieve something like this?
Yes this is it:
and to add or not white spaces at start and the end of matches, would be this:
The regexp has been little improved in this version by the original answer.
Anyway, the nice to me, is not the regexp, but the way preg_match_all and preg_replace have been used to manage placeholders replacements on the string, with a foreach
Needed result:
any instance like
<https://blahblahblah>
need to be switched into
https://blahblahblah
where could exist into a string, also something like this:
<https://blahblah>blublublu>
So, into same php file (it is divided to make it more clear):
Code: Select all
$string = 'Today at <-thiscanbeanyassortmentofanyrandomchars-><http://thiscanbeanyassortmentofanyrandomchars>it\'s raining but it is ok <b style="color:red">for me because</b> i\'m a <a href="https://www.google.com/search?q=snail">snail</a>
blah blah. Blahblah<blahblah><blahblah>. blah blah
blah blah blah<https://blahblahblah>blahblahblah>
Blah<https://blahblah> 12345 <https://blahblah<>blahblah>blahblahblah <https://blahblah>blublublu> <https://blah> <blah>
blah blah blah<blah><https://blahblahblah><blahblahblah <https://blahblahmissing <https://anotherblahblah>';
print_r($string); echo '<br /><br /> <-------> <br /><br />';
Code: Select all
$s = preg_match_all('/( ?)<{1}(ftp|https?):\/\/(.[^<]*?)>{1}( ?)/ui', $string, $matches, PREG_SET_ORDER);
$s = preg_replace('/<{1}(ftp|https?):\/\/(.[^<]*?)>{1}/ui', '#W3JB007PH#', $string, -1 ,$cr);
if($cr > 0){
foreach($matches as $m => $mv){
// $mv could be used to manipulate each match as more like, doing things here
$s = preg_replace('/\#W3JB007PH\#/u', ' ' . $matches[$m][2] . '://' . $matches[$m][3] . ' ', $s, 1); // one x time, in order, add spaces at star and end of the placeholder/match
// $s = preg_replace('/\#W3JB007PH\#/u', $matches[$m][1] . $matches[$m][2] . '://' . $matches[$m][3] . $matches[$m][4], $s, 1); // one x time, in order, re-add white spaces, only if there are found
//print_r($s);echo'<br />';
}
}
echo $s;
Do you know if there is a faster and shorter version of php code, to achieve something like this?
Yes this is it:
Code: Select all
$replaced = preg_replace('#( ?)\<{1}(ftp|https?)(:\/\/)(.[^<]*?)\>{1}( ?)#ui', ' \2\3\4 ', $string);
Code: Select all
$replaced = preg_replace('#( ?)\<{1}(ftp|https?)(:\/\/)(.[^<]*?)\>{1}( ?)#ui', '\1\2\3\4\5', $string);