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

parse string , return hyperlinks

P: n/a
I have a string with the following information.

$myString = "This is my example string please. Please visit
http://www.mylink.com . More example text goes here. another link
http://www.link2.com . more text.. blah..";

What I'm wanting is a function to return the following string :

$myString = This is my example string please. Please visit <a
href'http://www.mylink.com'>link</a> . More example text goes here.
another link <a href='http://www.link2.com'>link</a> . more text.. blah..";

Each string may contain 0 or more http:// instances. The function would
only parse on an http:// match.

I know this could be done with regular expressions, but I'm not sure how
to insert the modified data back into the original string at the correct
location.

Any help would be appreciated.

Thanks,

--
_______ - ----------------------------------------------/-//- -
|: | (_) o jesse @ jesseward . com
|. |____ __
| | :|(__)
|_______| .| - -------------------------- -- - - -
|_____|
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Jesse" <je*****@SPAMjesseward.com> wrote in message
news:10**************@nws01ykf.rim.net...
I have a string with the following information.

$myString = "This is my example string please. Please visit
http://www.mylink.com . More example text goes here. another link
http://www.link2.com . more text.. blah..";

What I'm wanting is a function to return the following string :

$myString = This is my example string please. Please visit <a
href'http://www.mylink.com'>link</a> . More example text goes here.
another link <a href='http://www.link2.com'>link</a> . more text.. blah..";
Each string may contain 0 or more http:// instances. The function would
only parse on an http:// match.

I know this could be done with regular expressions, but I'm not sure how
to insert the modified data back into the original string at the correct
location.

Any help would be appreciated.

Thanks,

--
_______ - ----------------------------------------------/-//- -
|: | (_) o jesse @ jesseward . com
|. |____ __
| | :|(__)
|_______| .| - -------------------------- -- - - -
|_____|


$myString = "This is my example string please. Please visit
http://www.mylink.com . More example text goes here. another link
http://www.link2.com . more text.. blah..";
$myString = preg_replace("|(http://\S*)|i","<a
href=\"$1\">link</a>",$myString);
Jul 17 '05 #2

P: n/a
kingofkolt wrote:
$myString = "This is my example string please. Please visit
http://www.mylink.com . More example text goes here. another link
http://www.link2.com . more text.. blah..";
$myString = preg_replace("|(http://\S*)|i","<a
href=\"$1\">link</a>",$myString);


thanks! works great.

--
_______ - ----------------------------------------------/-//- -
|: | (_) o jesse @ jesseward . com
|. |____ __
| | :|(__) www . jesseward . com
|_______| .| - -------------------------- -- - - -
|_____|
Jul 17 '05 #3

P: n/a
While the city slept, kingofkolt (je**********@comcast.net) feverishly
typed...
"Jesse" <je*****@SPAMjesseward.com> wrote in message
news:10**************@nws01ykf.rim.net...
I have a string with the following information.

$myString = "This is my example string please. Please visit
http://www.mylink.com . More example text goes here. another link
http://www.link2.com . more text.. blah..";

What I'm wanting is a function to return the following string :

$myString = This is my example string please. Please visit <a
href'http://www.mylink.com'>link</a> . More example text goes here.
another link <a href='http://www.link2.com'>link</a> . more text..
blah..";

Each string may contain 0 or more http:// instances. The function
would only parse on an http:// match.

I know this could be done with regular expressions, but I'm not sure
how to insert the modified data back into the original string at the
correct location.

$myString = "This is my example string please. Please visit
http://www.mylink.com . More example text goes here. another link
http://www.link2.com . more text.. blah..";
$myString = preg_replace("|(http://\S*)|i","<a
href=\"$1\">link</a>",$myString);


Works ok, unless the text is in 'normal' written language - where the full
stop (or period) would appear directly after the link (eg. "Please visit
http://www.mylink.com. More example text..."), in which case the closing
full stop will be a part of the link, and therefore result in a broken link.
Too late at night for me to start thinking how to overcome that, though! ;-)

Cheers,
Nige

--
Nigel Moss
http://www.nigenet.org.uk
Mail address not valid. ni***@DOG.nigenet.org.uk, take the DOG. out!
In the land of the blind, the one-eyed man is very, very busy!
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.