我如何得到preg_replace只是替换URL没有任何额外的空格或文本后发生的URL


How do I get preg_replace to just replace the URL without any additional spaces or text which occur after the URL?

下面是我的代码:

function prepare_url($text) {
    if (strpos($text, 'youtu') === FALSE) {
        $url = "''2";
    } else {
        $url = "<br><div id='".$playerid."'>Loading the player ...</div><script type='text/javascript'>jwplayer('".$playerid."').setup({flashplayer:'/jwplayer/player.swf',file:'".$text."',height:240,width:400});</script><br>";
        return $url;
    }
    $text = preg_replace("#(^|['n ])((['w]+?://['w'#$%&~.'-;:=,?@'[']+]*)(/['w'#$%&~/.'-;:=,?@'[']+]*)?)#is", "''1<a href='"''2'" target='"_blank'">".$url."</a>", $text);
    $text = preg_replace("#(^|['n ])(((www|ftp)'.['w'#$%&~.'-;:=,?@'[']+]*)(/['w'#$%&~/.'-;:=,?@'[']+]*)?)#is", "''1<a href='"http://''2'" target='"_blank'">".$url."</a>", $text);
    return $text;
}

此代码仅在YouTube URL被替换后没有发生任何事情时才有效。Youtube URL被替换后的所有内容都被添加为$text变量的一部分,使其无法使用。我能做些什么来让preg_replace在到达YouTube URL的末尾时停止?那是一碰到空格就开始的吗?

任何帮助都将是非常感激的:)

这个函数我写了一段时间将采取您输入的任何文本,并设置电子邮件链接或嵌入youtube视频的url。它将删除youtube url的尾随字符。希望能有所帮助:

function MakeAutoLink($string)
{
        /** Add http:// **/
    $string = preg_replace("/([^'w'/])(www'.[a-z0-9'-]+'.[a-z0-9'-]+)/i", "$1http://$2",$string);
        /** make all URLs links **/
    $string = preg_replace("/(['w]+:'/'/['w-?&;#~='.'/'@]+['w'/])/i","<a rel='"nofollow'" target='"_blank'" href='"$1'">$1</A>",$string);
        /** make all emails hot links **/
    $string = preg_replace("/(['w-?&;#~='.'/]+'@('[?)[a-zA-Z0-9'-'.]+'.([a-zA-Z]{2,3}|[0-9]{1,3})(']?))/i","<A HREF='"mailto:$1'">$1</A>",$string);
        /** The Regular Expression filter **/
    $youtube_video = "/v=(.+?)&/";
        /** Check if there is a url in the text **/
    $video = preg_match_all($youtube_video, $string, $match);
    if ($video)
        { 
            $links=$match[0]; 
        for ($key=0;$key<$video;$key++) 
            { 
                $newstring = $string.'<br /><br /><iframe title="YouTube video player" class="youtube-player" type="text/html" width="320" height="185" src="http://www.youtube.com/embed/'.substr($links[$key], 2,11).'" frameborder="0" allowFullScreen></iframe><br />';
                return $newstring;
            } 
        } 
    return $string;
}

把这个放到你的服务器上,然后导航到它进行测试:

video.php

    <?PHP
function MakeAutoLink($string)
    {
            /** Add http:// **/
        $string = preg_replace("/([^'w'/])(www'.[a-z0-9'-]+'.[a-z0-9'-]+)/i", "$1http://$2",$string);
            /** make all URLs links **/
        $string = preg_replace("/(['w]+:'/'/['w-?&;#~='.'/'@]+['w'/])/i","$1",$string);
        $youtube_video = "/v=(.+?)&/";
            /** Check if there is a url in the text **/
        $longurl = preg_match_all($youtube_video, $string, $match);
        if ($longurl)
            { 
                $links=$match[0]; 
            for ($key=0;$key<$longurl;$key++) 
                { 
                    $newstring = "http://www.youtube.com/watch?v=".substr($links[$key], 2,11);
                    return $newstring;
                } 
            } 
        return $string;
    }
?>
<!DOCTYPE html>
<html lang="en">
  <head>
  </head>
  <body>
 <?PHP
 if(isset($_POST['video']))
    {
        echo MakeAutoLink($_POST['video']);
    }
    else
        { 
 ?> 
 Try these two links:<br>
 http://www.youtube.com/watch?v=1rqQQkcB0MY<br><br>
 http://www.youtube.com/watch?v=1rqQQkcB0MY&some more text<br>
 <div align="center">
 <form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>"> 
    <label>video:</label>
    <input type="text" title="Youtube video" name="video" size="60" value="" autocomplete="off" autofocus>
    <br><br>    
    <input class="login" type="submit" name="login" value="Get Video">
 </form>
 </div> 
 <?PHP
 }
 ?>
  </body>
</html>

如果在视频id后面有其他选项,使用"&",这将转换url。如果您希望它在找到第一个空格时立即停止,请将/v=(.+?)&/替换为/v=(.+?)/,并确保在和/

之间留出空白。