By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,564 Members | 804 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,564 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.