469,602 Members | 1,727 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

how to replace urls in a document (with regular expression)

Hi-

I'm completely stumped. I'm trying to write some code that will parse
a file and rewrite it with all URLs replaced by something else.

For example: if the file looks like this:

<b>click here</b><a href="http://www.cnn.com">click me</a>

... then the output should be this:

<b>click here</b><a href="http://mysite?myurl=www.cnn.com">click
me</a>

Anyhelp on how to accomplish such thing would be really appreciated -
I spent hours on this and I'm completely stuck.

TIA,

JJ
Jul 16 '05 #1
3 6526
Curious Expatriate wrote:
Hi-

I'm completely stumped. I'm trying to write some code that will parse
a file and rewrite it with all URLs replaced by something else.

For example: if the file looks like this:

<b>click here</b><a href="http://www.cnn.com">click me</a>

.. then the output should be this:

<b>click here</b><a href="http://mysite?myurl=www.cnn.com">click
me</a>


If I'm not mistaken, it's a string added to a string.

//define the website url
$string="http://www.cnn.com"
// strip the text http:// (7 characters)
$urlvariable=substr($string,7)
//put it correctly together
"http://mysite?myurl=".$urlvariable

Should do what you want, right?

/Andreas

--
Registeret Linux user #292411
Jul 16 '05 #2
Andreas Paasch wrote:
Curious Expatriate wrote:
Hi-

I'm completely stumped. I'm trying to write some code that will parse
a file and rewrite it with all URLs replaced by something else.

For example: if the file looks like this:

<b>click here</b><a href="http://www.cnn.com">click me</a>

.. then the output should be this:

<b>click here</b><a href="http://mysite?myurl=www.cnn.com">click
me</a>


If I'm not mistaken, it's a string added to a string.

//define the website url
$string="http://www.cnn.com"
// strip the text http:// (7 characters)
$urlvariable=substr($string,7)
//put it correctly together
"http://mysite?myurl=".$urlvariable

Should do what you want, right?

/Andreas


Oops, doing this with regular expressions is not in my knowledge base,
sorry.

--
Registeret Linux user #292411
Jul 16 '05 #3
Curious Expatriate wrote:
Hi-

I'm completely stumped. I'm trying to write some code that will parse
a file and rewrite it with all URLs replaced by something else.

For example: if the file looks like this:

<b>click here</b><a href="http://www.cnn.com">click me</a>

.. then the output should be this:

<b>click here</b><a href="http://mysite?myurl=www.cnn.com">click
me</a>

Anyhelp on how to accomplish such thing would be really appreciated -
I spent hours on this and I'm completely stuck.

TIA,

JJ


Try something like:
$x=preg_replace('/(http\:\/\/\S+)/i',"http://mysite?myurl=$1"",$x);

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jane Doe | last post: by
24 posts views Thread by Wim Roffal | last post: by
12 posts views Thread by Brian | last post: by
5 posts views Thread by Casey | last post: by
4 posts views Thread by jgabbai | last post: by
9 posts views Thread by Salve =?iso-8859-1?Q?H=E5kedal?= | last post: by
1 post views Thread by NvrBst | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.