0

I am using this code:

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

So that I can turn the link beneath into a clickable link.

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 works for a part... It makes a link of http://upload.wikimedia.org/wikipedia/commons/f/f2/Bill_Clinton but the rest stays plain text. How can I make it work for the whole link? So you see a comma too... How can I make that a link?

4

1 回答 1

1

此外,我试图在 URL 的末尾考虑标点符号(以便我们不包括它)。

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


?>

输出

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.
于 2013-07-25T17:31:59.483 回答