preg_match_all and preg_replace - regexp placeholders replacement example

User avatar
axew3
w3all User
w3all User
Posts: 2883
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

preg_match_all and preg_replace - regexp placeholders replacement example

Post by axew3 »

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):

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;
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:

Code: Select all

$replaced = preg_replace('#( ?)\<{1}(ftp|https?)(:\/\/)(.[^<]*?)\>{1}( ?)#ui', ' \2\3\4 ', $string);
and to add or not white spaces at start and the end of matches, would be this:

Code: Select all

$replaced = preg_replace('#( ?)\<{1}(ftp|https?)(:\/\/)(.[^<]*?)\>{1}( ?)#ui',  '\1\2\3\4\5', $string);
User avatar
axew3
w3all User
w3all User
Posts: 2883
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: preg_match_all and preg_replace - regexp placeholders replacement example

Post by axew3 »

Extract from a string, into an array, all images URLs:

Code: Select all

$string = ' blah blah blah ANYTHINGCANBEHEREhttps://www.google.com/testyryr533443t.jpgANYTHINGCANBEHERE 
hjhj https://www.google.com/test.php fsfsf http://www.google.com/te s_t.JPG ANYTHING<https://pbs.twimg.com/media/EjgPYArWoAAUgPO?format=gif&name=900x900>http://www.google.com/test.gifhttp://www.google.com/dodacom?pic32=gif&stuff=lalala  httpS://www.google.com/whatever.png
https://www.xxxxxxxxxx.com/xxxxxx/pngx-xxxx/xxxxxx/x-xxxr-xxxx-gifx-xxx-xxxxxx-xxxxxx-xxxxx-x-xx-xxxx-xxx-xxpng http://www.google.com/dodacom?pic=jpg&stuff=lalala<https://pbs.twimg.com/media/113344WoAAUgPO?format=jpeg&name=900x900>
fehfiwhfeiw120270 blabla http://www.thexxx.xxx/xxxx/203012131/XXXX/444444444/-1/xxxxxxxxxxxx4444%3FTitle%3Dxxx-xxxxxxy-xxf-xxxxxl-xxxx-xxxxx&xx=xx&xx=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=xxxxxxxxxxxxxxxxx-xxxxxxxxxxxxePng';
Needed result:

Code: Select all

Array
(
    [0] => https://www.google.com/testyryr533443t.jpg
    [1] => http://www.google.com/te s_t.jpg
    [2] => https://pbs.twimg.com/media/ejgpyarwoaaugpo?format=gif
    [3] => http://www.google.com/test.gif
    [4] => http://www.google.com/dodacom?pic32=gif
    [5] => https://www.google.com/whatever.png
    [6] => http://www.google.com/dodacom?pic=jpg
    [7] => https://pbs.twimg.com/media/113344woaaugpo?format=jpeg
)

Code:

Code: Select all

$s = preg_match_all('/(https?:\/\/)(.[^< ]*?\/).[^< =]*?[\w ]+?(\.jpg|\.jpeg|\.gif|\.png|[\w][\.|=]jpg|[\.|=]jpeg|[\.|=]gif|[\.|=]png){1}/ui', $string, $matches, PREG_SET_ORDER);

$res = array_column($matches, 0);
$res = array_map('strtolower',$res);

echo'<pre>';
print_r($res);
exit;
[UPDATED]
User avatar
axew3
w3all User
w3all User
Posts: 2883
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: preg_match_all and preg_replace - regexp placeholders replacement example

Post by axew3 »

This will never fail:
https://www.phpbb.com/community/viewtop ... #p15608741

any suggestion on improve it showing another way to accomplish with the same result, would be really appreciated!
This will never fail:
until someone do not find out a bug!
Post Reply