Regular Expression Question
Any regular expression expert could lend a hand on this follow problem.
Php Code for converting any text URLs into HTML Links Version #1: $str = preg_replace('#(^|\s)([a-z]+://([^\s\w/]?[\w/+=,])*)#is', '\\1<a target=_blank href="\\2">\\2</a>', $str); This one is nice, when URL is reasonable length. so http://www.site.com ---> <a href="http://www.site.com">http://www.site.com</a> But when the URL is extremely long, for example a download link with long verification key, the output link would break the layout and look ugly. Version #2: $str = preg_replace('#(^|\s)([a-z]+://([^\s\w/]?[\w/+=,])*)#is', '\\1<a target=_blank href="\\2">Link »</a>', $str); So in attempt to fix the previous problem, we can also convert the actually link text to "Link >>" http://www.site.com ---> <a href="http://www.site.com">Link >></a> But this one doesn't look as intuitive for the users. Version #3: $str = preg_replace('#(^|\s)([a-z]+://([^\s\w/]?[\w/+=,])*)#is', '\\1<a target=_blank href="\\2" class=fix-width-diaplay-block-overflow-hidden>\\2</a>', $str); This version I define the css to restrict the output html links to avoid layout breaks. class=fix-width-diaplay-block-overflow-hidden But this is not natural either. I am a bit picky on things. Is there a simple regular expression to truncate and keep the first 30 characters of link text in the first version, something like this: $str = preg_replace('#(^|\s)([a-z]+://([^\s\w/]?[\w/+=,])*)#is', '\\1<a target=_blank href="\\2">\\2 {0,30} </a>', $str); I know the {0,30} is probably wrong syntax, any expert here could point out the right way of achieving this desired output ? --------------- Here is function I am trying to improve: PHP Code:
|
This might help:
PHP Code:
PHP Code:
|
lmfao, actually, that code converts the entire text-string into one big link.. although, the link does go to the first URL found..
Hmm.. I actually understand the problem now.. don't have an answer, but found this RFC with some interesting details: Quote:
Still needs that push between "grabbing value" and "using shorter value".. which I don't know if you can do without using two operations. 1. find matching URL, place URL in tmpVar 2. create shorter version, place in tmpVar2 3. construct URL and return <a href="tmpVar">tmpVar2</a> etc.. |
wow.. just noticed the board does that automatically,
1. find out what sort of board this is 2. inspect code 3. ..? 4. Profit! |
Isn't there a PHP module or class splitting URLs into their parts e.g. host, tld, domain, query string etc. pp.?
You could than just grab the host.domain.tld-part to put between Code:
<a href="http://veryverylongurlwithbellsandwhistles">http://some.host.domain.tld</a> |
Isn't there a PHP module or class splitting URIs into their parts e.g. host, tld, domain, query string etc. pp.?
You could than just grab the host.domain.tld-part to put between Code:
<a href="http://veryverylongurlwithbellsandwhistles">http://some.host.domain.tld</a> |
I am looking for something "simplistic" -- one line of regular expression, run it once only.
Otherwise, there are quite a few ways to achieve the desired results, such as but not limited: 1. filter the same text twice, 1st run, create the hyper links, second run, make the link-text shorter and add ... after it. 2. use preg_replace_callback() instead of preg_replace() Thank you for the interests, time in replying :) $str = preg_replace('#(^|\s)([a-z]+://([^\s\w/]?[\w/+=,])*)#is', '\\1<a target=_blank href="\\2">\\2[<--I only want the first 50 letters from this \\2--] ...</a>', $str); |
If you're set on limiting it to only regex, you could do something like this:
Code:
$str = preg_replace('#(^|\s)(([a-z]+://([^\s\w/]?[\w/+=,]){0,50})([^\s\w/]?[\w/+=,])*)#is', '\\1<a target=_blank href="\\2">\\3</a>', $str); |
All times are GMT -5. The time now is 07:57 PM. |