473,394 Members | 1,759 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,394 software developers and data experts.

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 2295
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Ram Laxman | last post by:
Hi all, I have a text file which have data in CSV format. "empno","phonenumber","wardnumber" 12345,2234353,1000202 12326,2243653,1000098 Iam a beginner of C/C++ programming. I don't know how to...
24
by: | last post by:
Hi, I need to read a big CSV file, where different fields should be converted to different types, such as int, double, datetime, SqlMoney, etc. I have an array, which describes the fields and...
3
by: Jon Davis | last post by:
The date string: "Thu, 17 Jul 2003 12:35:18 PST" The problem: // this fails on PST DateTime myDate = DateTime.Parse("Thu, 17 Jul 2003 12:35:18 PST"); Help? Jon
14
by: Jon Davis | last post by:
I have put my users through so much crap with this bug it is an absolute shame. I have a product that reads/writes RSS 2.0 documents, among other things. The RSS 2.0 spec mandates an en-US style...
4
by: tshad | last post by:
Is there any difference between convert.ToDateTime and System.DateTime.Parse? I am using them both and they seem the same. Is one better to use than another? Thanks, Tom
6
by: mike | last post by:
Hello, I am trying to write some code to parse a sentence and hyperlink just the words in it. I used Aaron's code from an earlier question as a start. So far, all the code does below is...
29
by: gs | last post by:
let say I have to deal with various date format and I am give format string from one of the following dd/mm/yyyy mm/dd/yyyy dd/mmm/yyyy mmm/dd/yyyy dd/mm/yy mm/dd/yy dd/mmm/yy mmm/dd/yy
5
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.