Also, is this http/https urls only?
Main Topics
Browse All TopicsIf I have 200 characters of text that contain a url, what is the fastest way to search the text for a url and, if there is one, wrap it in an anchor tag?
Actually I'll have an array of text snippets that I need to loop through, if that will matter.
foreach($temptext as $key => $node) {
$text[$key] = $node->textContent;
}
At some point, I need to search for urls in each $node->textContent and convert them to clickable links.
I can figure out how to do it with a series of string functions that searches for http, extracts it, wraps it in an anchor tag, etc. but I'm sure there is a better (more efficient) way to do it.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Yes, http only.
I'm not sure if you want to process the text before or after it is assigned to the $text array, but here is a sample after it is assigned.
$text[1] = "I'm sitting at my desk typing.";
$text[2] = "http://mydomain.com";
$tex
I want to change $text[2] into <a href="http://mydomain.com"
and $text[3] into Check out this site I found <a href="http://thissiteifoun
Worked just like I need it to, thanks!
Quick question... what's the difference between your expression and this one produced by RegexBuddy
\b(https?)://[-A
There comment is "The final character class makes sure that if an URL is part of some text, punctuation such as a comma or full stop after the URL is not interpreted as part of the URL."
I know the basics, like [a-z] and wildcards, so maybe I should be more specific.
How is theres case insenstive without the trailing i?
Will yours include the punctuation in the link if the text is "Check out http://mydomain.com."?
I ask so next time I can try and get it right on my own.
The RegexBuddy version does not include pattern modifers. It could be enclosed in ! ... !i or similar when used.
My version was weak when it comes to catching the end of the url. It requires a space, or to be at the end of the string: ([ ]|$). You can easily add the dot: ([ \.]|$), but specifying any character NOT in the list of valid chars is probably best: ([^a-z0-9\.\-/\?&\+~_=%#;@
Business Accounts
Answer for Membership
by: cxrPosted on 2008-11-01 at 17:40:36ID: 22859545
Can you show a sample?