Reputation: 27
Change only url which contains change.com replace "/" with "-" and put .html at end
<a href="http://www.notchange.com/adf/i18n/wiki/" class="coasfs" >as3rc</a>
<a href="http://www.change.com/q/photoshopbattles/comnts/2n4jtb/psbattle_asgfdhj/" class="coasfs" >as3rc</a>
<a href="http://www.change.com/q/photottles/commes/" class="coefs" >ase3rc</a>
i need result link bellow
http://www.change.com/q-photoshopbattles-comments-2n4jtb-psbattle_asgfdhj.html
please help me i tried many time with regex but failed.
Upvotes: 0
Views: 1025
Reputation: 1482
Here's one way to accomplish this, but it uses Regexes in conjunction to PHP functions. This method will be simpler than a pure Regex solution.
$string = '<a href="http://www.notchange.com/adf/i18n/wiki/" class="coasfs" >as3rc</a>'
. '<a href="http://www.change.com/q/photoshopbattles/comnts/2n4jtb/psbattle_asgfdhj/" class="coasfs" >as3rc</a>'
. '<a href="http://www.change.com/q/photottles/commes/" class="coefs" >ase3rc</a>';
//The regex used to match the URLs
$pattern = '/href="(http:\/\/www.change.com\/)([^">]*)"/';
preg_match_all($pattern, $string, $matches, PREG_SET_ORDER);
foreach ($matches as $val) {
//trim the ending slash if exists, replace the slashes in the URL path width a -, and add the .html
$newUrl = $val[1] . str_replace("/", "-", trim($val[2], "/")) . '.html';
$string = str_replace($val[0], 'href="' . $newUrl . '"', $string);
}
echo $string;
I used the regex to help find the urls that need to be modified, then use some built-in functions that PHP has to finish the job.
Upvotes: 1