0

I am working on an automation for building Landingpages.

A copy/pastes from a word doc to a TinyMCE textarea which creates the in the output.

so if I copy/paste something like this:

This is my Website.

from a word doc - the output of it after sending the form will look like this:

This is my <a href="http://www.google.com">Website</a>.

I want to append to every link within an <a href> tag (only within an <a href> tag!) something like this:

?utm=foo_foo_foo

so it will look like this:

This is my <a href="http://www.google.com?utm=foo_foo_foo">Website</a>.

P.S: urls can end with "/" or without, this shouldn't matter, but should work both ways.

P.S2: TinyMCR adds the tags by itself (if you haven't noticed me mentioning it..,). I just need to append to a string that looks like this:

$string = "This is my <a href="http://www.google.com">Website</a>.";
2
  • 1
    any code? have you tried anything? Moreover, please, let us know that either you're generating these href and yielding page OR you've page and want to change all anchor tags? Commented Nov 1, 2015 at 12:25
  • No code, I went through regex and preg_replace tutorials, everything is basic and not accurate to my needs. and I'm not sure I understand your second question. Commented Nov 1, 2015 at 12:29

1 Answer 1

1

You should use a parser, not a regex for this.

$html = 'This is my <a href="http://www.google.com">Website</a>.';
$dom = new DOMDocument(); 
$dom->loadHTML($html);
$links = $dom->getElementsByTagName('a');
foreach($links as $link) {
    $link->setAttribute('href', $link->getAttribute('href') . '?utm=foo_foo_foo');
}
echo $dom->saveHTML();

Output:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>This is my <a href="http://www.google.com?utm=foo_foo_foo">Website</a>.</p></body></html>

If you had to use a regex you could do

$html = 'This is my <a href="http://www.google.com">Website</a>.';
echo preg_replace('~href=("|\')(.+?)\1~', 'href=$1$2?utm=foo_foo_foo$1', $html);

Output:

This is my <a href="http://www.google.com?utm=foo_foo_foo">Website</a>.

Both these approaches presume you never have a ? in the URL already..

Sign up to request clarification or add additional context in comments.

8 Comments

Oooh. ok. I'm new to PHP, Can you give me a short explanation before I try it and start exploring "parers" ? Just guessing, Parsers pars texts/variables... so what difference them from regex? From what I know "Regex" searches for a pattern in a text and can change/add stuff to it.
I am free to use whatever I want in this code, tell me what you think is better and why, anyway I'm gonna continue researching both now. Learning everyday something new B|.
There's a longer write up on parsers here, stackoverflow.com/questions/3577641/…. Parsers are cleaner and have predefined functions, also if a failure occurs with a parser it won't destroy, with regex you have possibility to go very wrong.
Yes, the parsers code looks more organized to be honest.. Thanks, I'll try this in a second and update you if it's working right.
See my previous last comment.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.