Replacing Text link as link with preg_replace

后端 未结 2 1304
南旧
南旧 2021-01-19 05:50

I seem to be having an issue replacing a text link with a link to the site posted, it isn\'t linking.

The code:

$status_text = preg_replace(\'#(\\A|[         


        
相关标签:
2条回答
  • 2021-01-19 06:04

    Can you just try this:

    function makeClickableLinks($s) {
      return preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" target="_blank">$1</a>', $s);
    }
    

    To test it, try this:

    echo makeClickableLinks('Hello world, you go to www.google.com to check! also http://www.google.com/ works too!');
    

    Demo here: http://codepad.viper-7.com/hkOdCM

    0 讨论(0)
  • 2021-01-19 06:20

    Here, try this:

    $status_text = preg_replace('|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '$1 <a href="$2">$3</a>', $status_text);
    echo $status_text;
    

    or to make it a little easer to read:

    $m = '|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
    $r = '$1 <a href="$2">$3</a>';
    
    $status_text = preg_replace($m,$r,$status_text);
    echo $status_text;
    

    EDIT -- Updating due to new info in the OP --

    Your hashtag regex does not take into account hashes in URLs, so let's fix that... Also, you should match for URLs before matching for hash-tags or plus-tags because otherwise you will mess up the links you create for the hash-tags and plus-tags

    $status_text = preg_replace('|(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '<a href="$1">$2</a>', $status_text);
    $status_text = preg_replace('|\B#([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
    $status_text = preg_replace('|\B\+([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
    

    Or to make it a little easier to read...

    $match_href = '|(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
    $match_hash = '|\B#([\d\w_]+)|i';
    $match_plus = '|\B\+([\d\w_]+)|i';
    $replace_url = '<a href="$1">$2</a>';
    $replace_tag = '<a href="http://url.com/pluslink/$1">$0</a>';
    
    $status_text = preg_replace($match_href, $replace_url, $status_text);
    $status_text = preg_replace($match_hash, $replace_tag, $status_text);
    $status_text = preg_replace($match_plus, $replace_tag, $status_text);
    

    EDIT AGAIN -- Adding a URL that might be helpful --

    You can test out regular expressions here: http://gskinner.com/RegExr/

    ANOTHER EDIT

    As per a comment/question, if you want to account for urls that lack the http protocol, use the following:

    $status_text = preg_replace('|((https?://)?([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '<a href="$1">$3</a>', $status_text);
    $status_text = preg_replace('|\B#([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
    $status_text = preg_replace('|\B\+([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
    

    Or to make it a little easier to read...

    $match_href = '|((https?://)?([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
    $match_hash = '|\B#([\d\w_]+)|i';
    $match_plus = '|\B\+([\d\w_]+)|i';
    $replace_url = '<a href="$1">$3</a>';
    $replace_tag = '<a href="http://url.com/pluslink/$1">$0</a>';
    
    $status_text = preg_replace($match_href, $replace_url, $status_text);
    $status_text = preg_replace($match_hash, $replace_tag, $status_text);
    $status_text = preg_replace($match_plus, $replace_tag, $status_text);
    

    EXAMPLE USAGE

    Input: (Text from DB -> $status_text)

    <!-- language-all: lang-none -->
    Hi, this is an example. This is a url http://stackoverflow.com/ 
    and this is a hash reference that we want to link to an internal 
    post #AwesomePost123 and this one is a plus reference we want to 
    link to an internal post +AwesomePost123 and finally an example 
    of a url without the http protocol www.stackoverflow.com
    

    Output: (After running through the regex)

    <!-- language-all: lang-none -->
    Hi, this is an example. This is a url <a href="http://stackoverflow.com/">stackoverflow.com</a> 
    and this is a hash reference that we want to link to an internal 
    post <a href="http://url.com/pluslink/AwesomePost123">#AwesomePost123</a> and this one is a plus reference we want to 
    link to an internal post <a href="http://url.com/pluslink/AwesomePost123">+AwesomePost123</a> and finally an example 
    of a url without the http protocol <a href="www.stackoverflow.com">www.stackoverflow.com</a>
    
    0 讨论(0)
提交回复
热议问题