469,648 Members | 1,413 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

parse string , return hyperlinks

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
3 2117
"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
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
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.

Similar topics

22 posts views Thread by Ram Laxman | last post: by
3 posts views Thread by Jon Davis | last post: by
29 posts views Thread by gs | last post: by
AdrianH
5 posts views Thread by AdrianH | last post: by
AdrianH
1 post views Thread by AdrianH | 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.