472,989 Members | 3,047 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

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 6713
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jane Doe | last post by:
Hi, I need to search and replace patterns in web pages, but I can't find a way even after reading the ad hoc chapter in New Rider's "Inside JavaScript". Here's what I want to do: function...
24
by: Wim Roffal | last post by:
Is there a possibility to do a string replace in javascript without regular experessions. It feels like using a hammer to crash an egg. Wim
12
by: Brian | last post by:
I want to use regxp to check that a form input contains at least 1 non-space charcter. I'd like to only run this if the browser supports it. For DOM stuff, I'd use if (documentGetElementById) {}...
5
by: Casey | last post by:
Hello, Can someone give me specific code to replace text on a page using server side javascript? I need to use server-side because I need the output to be recognized in the final HTML so that...
4
by: jgabbai | last post by:
Hi, What is the best way to white list a set of allowable characters using regex or replace? I understand it is safer to whitelist than to blacklist, but am not sure how to go about it. Many...
3
by: TOXiC | last post by:
Hi everyone, First I say that I serched and tryed everything but I cannot figure out how I can do it. I want to open a a file (not necessary a txt) and find and replace a string. I can do it...
9
by: Salve =?iso-8859-1?Q?H=E5kedal?= | last post by:
What is the best regular expression for finding urls in plain text files? (By urls I mean http://www.something.com, but also www.something.com, or salve@somewhere.com) Salve
1
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
4
Dasty
by: Dasty | last post by:
My problem: My users will write some text into <textarea> area. All I want to do with javascript is to take that textarea.value and replace all url links there with regular <a href> and then put...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.