Link to home
Start Free TrialLog in
Avatar of EMB01
EMB01Flag for United States of America

asked on

Auto-Appending Query String to URL's on Condition

How do I append a querystring to a URL when the URL is inside a bunch of HTML? Furthermore, how do I append upon two conditions; one, if the URL doesn't contain a "?" and, two, if the URL does contain a "?"

Example attached.
<HTML>
<p>The following link should change to "http://www.emarketbuilders.com/?var=val."</p>
<a href="http://www.emarketbuilders.com">Link without original querystring</a>
<br />
<p>The following link should change to "http://www.emarketbuilders.com/?var=val&var1=val1."</p>
<a href="http://www.emarketbuilders.com/?var1=val1">Link with original querystring</a>
<br />
<p>So, there should be two separate variables two append for two different conditions. The questions are: </p>
<ul>
<li>How do I find URL's from a variable containing HTML (like this one).</li>
<li>How do I replace the URL's based on the result?</li>
</ul>
<p>Thank you!</p>
</HTML>

Open in new window

Avatar of Member_2_4694817
Member_2_4694817


$newurl = $oldurl . (strpos($oldurl,"?")>0 ? "&amp;" : "?") . "var=val";

Open in new window

Avatar of EMB01

ASKER

The thing about it is, there may be "?" riddled throughout the HTML - the script must find "?" only in the URL's. Got anything for that?
Oh, that was only part of your question.

try as below to first prepend var=val& with existing question marks and then append ?var=val for non-existing. This version works only for urls enclosed in quotes, not in apostrophes, but you can easily adapt that
Of course, relative urls are omitted...
$tmp = preg_replace('/"([a-z]+:\/\/[^\?"]+\?)([^"]*)"/', "$1var=val&amp;$2", $text);
$result = preg_replace('/"([a-z]+:\/\/[^\?"]+)"/', "$1?var=val", $tmp);

Open in new window

Avatar of EMB01

ASKER

Thanks, but how exactly does that work? Do you mean it prepends this "?var=val" and appends this "var=val&"
The first replaces
quote + protocol schme (e.g. http) + "://" + sequence of anything except question mark or quote + question mark + (place to insert new data) + sequence of anything except quote + quote
It inserts "var=val&" where marked, thus prepending before the old params

The second replaces
quote + protocol schme  + "://" + sequence of anything except question mark or quote +  (insert new data here)  + quote
It inserts "?var=val" because there was no param yet

It is essential to keep the order as above!

Avatar of EMB01

ASKER

I thinking I'm getting it... What's the "$1" and "$2" that I notice from "$1var=val&amp;$2"
$1 is what matches teh regular expression between the first pair of parentheses, $2 the second.
Now that you mention it, the quotes shouldbelong nito the parentheses, i.e. replace
'/"(...)"/' with '/("...")/'
$tmp = preg_replace('/("[a-z]+:\/\/[^\?"]+\?)([^"]*")/', "$1var=val&amp;$2", $text);
$result = preg_replace('/("[a-z]+:\/\/[^\?"]+)(")/', "$1?var=val$2", $tmp);

Open in new window

Avatar of EMB01

ASKER

So, does that mean that a URL has to be like "http://www.emarketbuilders.com/" and not "http://www.emarketbuilders.com"
No, "http://www.emarketbuilders.com" would become "http://www.emarketbuilders.com?var=val".
Is that not ok?

And I note that I misused quotes *again*. *sigh*.
Either use '$1?var=val$2' or "\$1?var=val\$2" etc.
I assume you prefer the second in order to keep the var=val stuff variable.
Sorry for my lots of misspelling - it's getting late.

Avatar of EMB01

ASKER

It's okay! What I mean to ask is: Does the preg_replace function search for "http://www.emarketbuilders.com/" or "http://www.emarketbuilders.com" or both?
It searches for anything that is inside quotes (as in href="...") and starts with e.g. http:// or https:// or ftp:// or ...
Apart from that, the url need not be "correct" in any respect.
Then one of the statements looks for a "?" inside and inserts after that, the other works for the case wthout previous "?".
Avatar of EMB01

ASKER

Alright, so if I have a variable "$variable" and it has this in it:
<HTML>
<p><a href="http://www.emarketbuilders.com">emarketbuilders.com</a></p>
</HTML>

It will be turned into:
<HTML>
<p><a href="http://www.emarketbuilders.com/?var=val">emarketbuilders.com</a></p>
</HTML>

Via this:
$tmp = preg_replace('/("[a-z]+:\/\/[^\?"]+\?)([^"]*")/', "\/$1var=val&amp;$2\/", $variable);
$result = preg_replace('/("[a-z]+:\/\/[^\?"]+)(")/', "\/$1?var=val$2\/", $tmp);

Then, if I want to echo the result, I would do this:
echo $result;

So, basically "$result" is the new version of "$variable." Right?
ASKER CERTIFIED SOLUTION
Avatar of Member_2_4694817
Member_2_4694817

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of EMB01

ASKER

How about if I want to add multiple variables to the querystring? How would the code change, then? Note: The points have been raised by 150.
Avatar of EMB01

ASKER

Thanks, why did you stop helping? I raised the points! Have a good day.