I need to do a search and replace (possibly using a regular expression search) to append ?pb=1 to every link found in a string. For example in the following html I want to make:-
href=“http://mynerja.com/about/balcon-de-europa-nerja/”
into
href=“http://mynerja.com/about/balcon-de-europa-nerja/?pb=1”.
I need to do this dynamically in PHP for all the links in the string.
Thanks for any suggestions and I hope the above makes sense.
<li class="page_item page-item-1225"><a class="fadeThis" href="http://mynerja.com/about/balcon-de-europa-nerja/"><span>Balcon de Europa, Nerja</span></a></li>
<li class="page_item page-item-1452"><a class="fadeThis" href="http://mynerja.com/about/beaches-in-nerja/"><span>Beaches in Nerja</span></a></li>
<li class="page_item page-item-2958"><a class="fadeThis" href="http://mynerja.com/about/charities-in-nerja/"><span>Charities in Nerja CAS, Nerja Donkey Sanctuary, CUDECA</span></a></li>
Hi there,
If we define a ‘link’ as what comes between href=" and the closing "
then we can use the following regex:
/href="(.*?)"/
This would give us:
<?php
$String ='<li class="page_item page-item-1225"><a class="fadeThis" href="http://mynerja.com/about/balcon-de-europa-nerja/"><span>Balcon de Europa, Nerja</span></a></li>
<li class="page_item page-item-1452"><a class="fadeThis" href="http://mynerja.com/about/beaches-in-nerja/"><span>Beaches in Nerja</span></a></li>
<li class="page_item page-item-2958"><a class="fadeThis" href="http://mynerja.com/about/charities-in-nerja/"><span>Charities in Nerja CAS, Nerja Donkey Sanctuary, CUDECA</span></a></li>';
echo preg_replace('/href="(.*?)"/s', 'href="$1?pb=1"', $String);
which should hopefully do what you want.
Thanks Pullo - that looks like exactly what I need. I will give it a try and report back.
Finally had a chance to try this out this morning and it worked first time.
Thanks again for your help.
You’re welcome.
Thanks for taking the time to follow up
Just keep in mind a caveat. This works fine as long as none of the links in your string have a querystring on them already.
Good point.
How would one go about writing a reg ex that took that into account?
same regex, but feed the result into a callback function with [FPHP]preg_replace_callback[/FPHP]
Hi StarLion,
What do you think about this solution?
It works, but am I horribly overcomplicating things?
<?php
$String ='<li class="page_item page-item-1225"><a class="fadeThis" href="http://mynerja.com/about/balcon-de-europa-nerja/?param=1"><span>Balcon de Europa, Nerja</span></a></li>
<li class="page_item page-item-1452"><a class="fadeThis" href="http://mynerja.com/about/beaches-in-nerja/"><span>Beaches in Nerja</span></a></li>
<li class="page_item page-item-2958"><a class="fadeThis" href="http://mynerja.com/about/charities-in-nerja/"><span>Charities in Nerja CAS, Nerja Donkey Sanctuary, CUDECA</span></a></li>';
function processURL($matches){
$queryString = (array_key_exists('query', parse_url($matches[2])))? "&pb=1" : "?pb=1";
return "$matches[1]$matches[2]$queryString$matches[3]";
}
echo preg_replace_callback('/(<li.*?href=")(.*?)(".*?<\\/li>)/', 'processURL', $String);
?>
You will notice I’ve attached a querystring to the first link.
Would be grateful for any feedback.
keep the same regex - you only care about the href attributes, so ‘href=“(.*?)”’ will suffice for a pattern, and then the replace would just be
'href="'.$matches[1].$querystring.'"'
(the href and quotes are static elements - so you dont need to pattern them)
Oh cool!
That’s much cleaner. Thanks!
Here’s the revised code in case the OP can make use of it:
<?php
$String ='<li class="page_item page-item-1225"><a class="fadeThis" href="http://mynerja.com/about/balcon-de-europa-nerja/?param=1"><span>Balcon de Europa, Nerja</span></a></li>
<li class="page_item page-item-1452"><a class="fadeThis" href="http://mynerja.com/about/beaches-in-nerja/"><span>Beaches in Nerja</span></a></li>
<li class="page_item page-item-2958"><a class="fadeThis" href="http://mynerja.com/about/charities-in-nerja/"><span>Charities in Nerja CAS, Nerja Donkey Sanctuary, CUDECA</span></a></li>';
function processURL($matches){
$queryString = (array_key_exists('query', parse_url($matches[1])))? "&pb=1" : "?pb=1";
return 'href="'.$matches[1].$queryString.'"';
}
echo preg_replace_callback('/href="(.*?)"/s', 'processURL', $String);
?>