php - how to replace group of texts in a link -
i have regex detects type of link.the detection part works fine replacing part dosen't work.i tried replace on http://regex101.com/
the code:
$link="cover_photos_s/yasinallana1984751717_post_notif_aurora_kuenzli_big.jpg";  //regex $reg_for_key="~((cover_photos_s\/)([a-za-z0-9]+)(_post_notif_)(\w+)(.[a-z]+))~i";  if(preg_match_all($reg_for_key,$link)) { //the replace string goes here }   i need replace _post_notif_ in src _thumb_ .how do it?
solved thnx @onlinecop
in below regex, replace second captured group _thumb_,
^(cover_photos_s\/[a-z0-9a-z]+)(_\w+?_\w+?_)(.*)$     your php code be,
<?php $link = "cover_photos_s/yasinallana1984751717_post_notif_aurora_kuenzli_big.jpg"; $reg_for_key = "~^(cover_photos_s\/[a-z0-9a-z]+)(_\w+?_\w+?_)(.*)$~"; $replacement = "$1_thumb_$3"; echo preg_replace($reg_for_key, $replacement, $link); ?>  //=> cover_photos_s/yasinallana1984751717_thumb_aurora_kuenzli_big.jpg     explanation:
(cover_photos_s\/[a-z0-9a-z]+)captures upto_present before stringpost.(_\w+?_\w+?_)underscore, matches shortest word characters upto first underscore, matches string_post. again matches upto stringnotifbecause of?operator after+symbol makes regex engine match shortest possibility plus following_symbol. in turn regex engine capture second group of characters.- all characters next them captured third group.
 - in replacement part, replacing second group string give desired output.
 
Comments
Post a Comment