Replacing Text link as link with preg_replace

后端 未结 2 1306
南旧
南旧 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:20

    Here, try this:

    $status_text = preg_replace('|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '$1 $3', $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 $3';
    
    $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', '$2', $status_text);
    $status_text = preg_replace('|\B#([\d\w_]+)|i', '$0', $status_text);
    $status_text = preg_replace('|\B\+([\d\w_]+)|i', '$0', $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 = '$2';
    $replace_tag = '$0';
    
    $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', '$3', $status_text);
    $status_text = preg_replace('|\B#([\d\w_]+)|i', '$0', $status_text);
    $status_text = preg_replace('|\B\+([\d\w_]+)|i', '$0', $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 = '$3';
    $replace_tag = '$0';
    
    $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)

    
    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)

    
    Hi, this is an example. This is a url 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
    

提交回复
热议问题