0

I am trying to convert a block of text that contains html text - i'd like to find all http links and convert them for link tracking purposes.

So eg anything like this in a string would be converted to the latter

<a href="http://www.google.com">Some Link</a>

<a href="http://www.mysite.com/tracking.php?url=www.google.com">Some Link</a>

Can anyone how to do this taking into account the original string will consists of all sorts of html, images etc..

1
  • Sorry i misunderstand question Commented Apr 2, 2012 at 11:00

2 Answers 2

2

Use this regex : (UPDATED)

<?php

$str = '<h1>Page Title</h1><a href="http://www.google.com/">Google</a>';
$text = preg_replace("/href=\"http\:\/\/([a-zA-Z0-9\-]+\.[a-zA-Z0-9]+\.[a-zA-Z]{2,3}(\/*)?)/","href=\"http://www.mysite.com/tracking.php?url=$1\"",$str);

echo $text;

?>

Outputs :

<h1>Page Title</h1><a href="http://www.mysite.com/tracking.php?url=www.google.com/"">Google</a>
Sign up to request clarification or add additional context in comments.

4 Comments

I get the following error: Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in...
My code below.. (cant seem to get the formatting correct??) $str = '<h1>Page Title</h1><a href="google.com">Google</a>'; $text = preg_replace('href=\"http\://([a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?)\"', 'href=\"YOUR_TRACKING_URL=$1\"', $str); echo $text;
just a quick one.. How would I amend the preg_replace to only replace href's that do not have the 'clients' in the href? (but keeping the existing rule as well)
@Gaz Could you give me an example of a URL containing 'clients'? (I mean you're obviously not interested in excluding clients.com, but something along the lines of http://www.yourdomain.com/clients/some_page.php, right? that's what I want so as to make it more specific) :-)
0
$str = '<h1>Page Title</h1><a href="http://www.google.com">Google</a>';
$text = preg_replace('href=\"http\://([a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?)\"', 'href=\"YOUR_TRACKING_URL=$1\"', $str);
echo $text;

Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in /home/.... (sorry for the duplication)

Comments

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.