parse string , return hyperlinks

This is a discussion on parse string , return hyperlinks within the PHP Language forums, part of the PHP Programming Forums category; I have a string with the following information. $myString = "This is my example string please. Please visit http://www....


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-10-2004
Jesse
 
Posts: n/a
Default 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
|. |____ __
| | :|(__)
|_______| .| - -------------------------- -- - - -
|_____|
Reply With Quote
  #2 (permalink)  
Old 08-10-2004
kingofkolt
 
Posts: n/a
Default Re: parse string , return hyperlinks

"Jesse" <jesseNO@SPAMjesseward.com> wrote in message
news:1092147903.64208@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);


Reply With Quote
  #3 (permalink)  
Old 08-10-2004
Jesse
 
Posts: n/a
Default Re: parse string , return hyperlinks

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
|_______| .| - -------------------------- -- - - -
|_____|
Reply With Quote
  #4 (permalink)  
Old 08-11-2004
nice.guy.nige
 
Posts: n/a
Default Re: parse string , return hyperlinks

While the city slept, kingofkolt (jessepNOSPAM@comcast.net) feverishly
typed...

> "Jesse" <jesseNO@SPAMjesseward.com> wrote in message
> news:1092147903.64208@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. nigel@DOG.nigenet.org.uk, take the DOG. out!
In the land of the blind, the one-eyed man is very, very busy!


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:14 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0