ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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 ?
/** * Allows you to output a large quantity of text, with a character limit.. useful for variable quantities. * @param string text * @param integer limit */ public static function makeSmaller($text, $limit) { if (strlen($text) > $limit) { return substr($text, 0, strrpos(substr($text, 0, $limit), ' ')) . '...'; } else { return $text; } }
Then.. something like..
PHP Code:
public static function hyperlink( $str ){ //Make output text presentably short $display_str = makeSmaller($str, 25);
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:
Originally Posted by RFC 3986
Appendix B. Parsing a URI Reference with a Regular Expression
As the "first-match-wins" algorithm is identical to the "greedy"
disambiguation method used by POSIX regular expressions, it is
natural and commonplace to use a regular expression for parsing the
potential five components of a URI reference.
The following line is the regular expression for breaking-down a
well-formed URI reference into its components.
The numbers in the second line above are only to assist readability;
they indicate the reference points for each subexpression (i.e., each
paired parenthesis). We refer to the value matched for subexpression
<n> as $<n>. For example, matching the above expression to
where <undefined> indicates that the component is not present, as is
the case for the query component in the above example. Therefore, we
can determine the value of the five components as
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);
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.