473,387 Members | 1,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 6735
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.