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....
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |. |____ __ | | :|(__) |_______| .| - -------------------------- -- - - - |_____| |
|
|||
|
"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); |
|
|||
|
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 |_______| .| - -------------------------- -- - - - |_____| |
|
|||
|
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! |