Regex not getting whole link at special characters

前端 未结 1 1738
囚心锁ツ
囚心锁ツ 2021-01-27 05:32

I am using this code:

$string = preg_replace(\"~(?!(?:https?://(?:www\\.)?|www\\.)(?:youtube\\.com)(?:https?://(?:www\\.)?|www\\.)[\\w./=?#-]+~i\", \'

        
相关标签:
1条回答
  • 2021-01-27 06:13

    Also I'm trying to account for punctuation at the end of a URL (so that we don't include it).

    <?php
    
    $string = "This works http://upload.wikimedia.org/wikipedia/commons/f/f2/Bill_Clinton%2C_Yitzhak_Rabin%2C_Yasser_Arafat_at_the_White_House_1993-09-13.jpg. This one should fail http://www.youtube.com/v/adlskdfjasopie. Although this one should fail as well http://youtu.be/adlkajdaslk.";
    
    $string = preg_replace("~(?!(?:https?://(?:www\.)?|www\.)(?:youtu))(?:https?://(?:www\.)?|www\.)[^\s]+[^.!?,\<\]\[\)(]~i",'<a href="$0">$0</a>',$string);
    
    
    ?>
    

    output

    This works <a href="http://upload.wikimedia.org/wikipedia/commons/f/f2/Bill_Clinton%2C_Yitzhak_Rabin%2C_Yasser_Arafat_at_the_White_House_1993-09-13.jpg. ">http://upload.wikimedia.org/wikipedia/commons/f/f2/Bill_Clinton%2C_Yitzhak_Rabin%2C_Yasser_Arafat_at_the_White_House_1993-09-13.jpg. </a>This one should fail http://www.youtube.com/v/adlskdfjasopie. Although this one should fail as well http://youtu.be/adlkajdaslk.
    
    0 讨论(0)
提交回复
热议问题