LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Regular Expression Question (https://www.linuxquestions.org/questions/programming-9/regular-expression-question-733194/)

yuye811 06-15-2009 08:13 PM

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:

public static function hyperlink$str ){

    
// match protocol://address/path/file.extension?some=variable&another=asf%
    
$str preg_replace('#(^|\s)([a-z]+://([^\s\w/]?[\w/+=,])*)#is''\\1<a target=_blank href="\\2">Link »</a>'$str);
    
    
// match www.something.domain/path/file.extension?some=variable&another=asf%
    
$str preg_replace('#(^|\s)((www|ftp)\.([^\s\w/]?[\w/+=,])*)#is''\\1<a target=_blank href="http://\\2">Link »</a>'$str);
    
    
// match name@address
    
$str preg_replace('#(^|\s)(([a-z0-9._%+-]+)@(([.-]?[a-z0-9])*))#is''\\1<a href="mailto:\\2">\\2</a>'$str);
    
    return 
$str;



grizly 06-15-2009 09:59 PM

This might help:

PHP Code:

/**
 * 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($text0strrpos(substr($text0$limit), ' ')) . '...';
    } else
    {
        return 
$text;
    }


Then.. something like..

PHP Code:

public static function hyperlink$str ){
    
//Make output text presentably short
    
$display_str makeSmaller($str25);

    
// match protocol://address/path/file.extension?some=variable&another=asf%
    
$str preg_replace('#(^|\s)([a-z]+://([^\s\w/]?[\w/+=,])*)#is''\\1<a target=_blank href="\\2">'.$display_str.' »</a>'$str);
    
    
// match www.something.domain/path/file.extension?some=variable&another=asf%
    
$str preg_replace('#(^|\s)((www|ftp)\.([^\s\w/]?[\w/+=,])*)#is''\\1<a target=_blank href="http://\\2">'.$display_str.' »</a>'$str);
    
    
// match name@address
    
$str preg_replace('#(^|\s)(([a-z0-9._%+-]+)@(([.-]?[a-z0-9])*))#is''\\1<a href="mailto:\\2">\\2</a>'$str); 

    return 
$str;



grizly 06-15-2009 10:19 PM

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.


^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
12 3 4 5 6 7 8 9

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

http://www.ics.uci.edu/pub/ietf/uri/#Related

results in the following subexpression matches:

$1 = http:
$2 = http
$3 = //www.ics.uci.edu
$4 = www.ics.uci.edu
$5 = /pub/ietf/uri/
$6 = <undefined>
$7 = <undefined>
$8 = #Related
$9 = Related

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

scheme = $2
authority = $4
path = $5
query = $7
fragment = $9

http://www.ietf.org/rfc/rfc3986.txt

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..

grizly 06-15-2009 10:20 PM

wow.. just noticed the board does that automatically,

1. find out what sort of board this is
2. inspect code
3. ..?
4. Profit!

Su-Shee 06-16-2009 07:39 AM

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>

Su-Shee 06-16-2009 07:45 AM

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 just know the Perl modules to archieve this without regex, so I just assume that there is something similar in PHP...)

yuye811 06-17-2009 12:28 AM

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);

angrybanana 06-19-2009 01:55 AM

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);
Basically it wraps everything up in another group, one group containing the {0,50} cutoff version, another encompassing both {0,50} and * combined.


All times are GMT -5. The time now is 07:57 PM.