So, the situation I’m currently in is a wee bit complicated (for me that is), but I’m gonna give it a try.
I would like to run to a snippet of HTML and extract all the links referring to my own domain. Next I want to append these URL’s with a predefined string of GET vars. For example, I want to append ‘?var1=2&var2=4’ to ‘http://www.domain.com/page/‘ thus creating ‘http://www.domain.com/page/?var1=2&var2=4‘.
The method I’m currently applying is a simple preg_replace function (PHP), but here is when it gets interesting. How do i create valid appended url’s when they already have some GET vars at the end? For example, it could create a url like this: ‘http://www.domain.com/page/?already=here&another=one?var1=2&var2=4‘
thus breaking the GET data.
So to conclude, what I’m looking for is a reg exp which can cope with these scenarios, create my extended url and write it back to the HTML snippet.
This is what I have so far:
$sHTML = preg_replace("'href=\"($domainURL.*?[\/$])\"'", 'href="\1' . $appendedTags . '"', $sHTML);
Thanks in advance
In addition to what Elazar Leibovich suggested, I’d parse the query string with
parse_str(), modify the resulting array to my needs and then usehttp_build_query()to rebuild the query string. This way you won’t have any duplicates within your query string and you don’t have to bother yourself with url-encoding your query-parts.The complete example would then look like (augmenting Elazar Leibovich code):