【问题标题】:How to get any url from the given data如何从给定数据中获取任何 url
【发布时间】:2026-01-26 06:45:02
【问题描述】:

我想从给定的数据中获取 url。

例如我有一个变量中的数据

$data = "is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, http://www.youtube.com/watch?v=mm78xlsADgc when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but";

现在我想从给定的变量$data 中获取这个 URL (http://www.youtube.com/watch?v=mm78xlsADgc)。请告诉我该怎么做?

【问题讨论】:

  • 你在这个网站上搜索过吗?
  • 是的,我在网站上搜索过,但没有得到解决方案,这就是我发布此问题的原因。
  • 您可以尝试查找 URL 验证正则表达式,例如*.com/questions/206059/php-validation-regex-for-url/…
  • 这是否从 $data 变量返回 url?我认为它将验证该字符串是否为 url。我说的对吗?
  • 我设法链接到该问题中的错误答案。我的意图是链接到使用正则表达式的已接受答案。在您的情况下,您将无法使用 filter_var,但您可以对 URL 进行模式匹配。

标签: php preg-match preg-match-all preg-split


【解决方案1】:

您应该filter_var() 这样做。

var_dump(filter_var('http://www.youtube.com/watch?v=', FILTER_VALIDATE_URL));

【讨论】:

  • 这会返回 $data 变量中的 url 吗?
  • 不,它没有。 OP 希望在字符串中查找 URL,而不是验证字符串是否为 URL。
【解决方案2】:

使用上面的this answer 和您的$data 中的模式:

preg_match_all(
    '#((?:http|https|ftp)://(?:\S*?\.\S*?))(?:\s|\;|\)|\]|\[|\{|\}|,|"|\'|:|\<|$|\.\s)#i',
    $data,
    $matches
);
var_dump($matches[1]);

See it in action on Ideone.

根据您要匹配的 URL 类型,您可能希望使用实际使用的模式,但概念是相同的。

【讨论】:

    【解决方案3】:

    试试这个

    $data = "is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, http://www.youtube.com/watch?v=mm78xlsADgc when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but";
    $array1 = explode(" ", $data);
    $searchword = 'http';
    $matches = array();
    foreach($array1 as $k=>$v) {
        if(preg_match("/\b$searchword\b/i", $v)) {
           echo $matches[$k] = $v;
        }
    }
    

    【讨论】:

      【解决方案4】:

      怎么样:

      preg_match_all(
          '#(https?://(?:\S+))#i',
          $data,
          $matches
      );
      

      \S 代表任何不是空格的字符。

      【讨论】:

        【解决方案5】:
        <?php  
        $data = "is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, http://www.youtube.com/watch?v=mm78xlsADgc when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but";
        
        preg_match('/(https?:.*?)\s+/si', $data, $url);
        
        echo $url[0];
        ?>
        

        DEMO

        【讨论】:

          【解决方案6】:

          使用此代码,它对我很有效

          $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
          
          // The Text you want to filter for urls
          $text = "is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, http://www.youtube.com/watch?v=mm78xlsADgc when an unknown printer took a galley of type and scrambled it to make a type   specimen book. It has survived not only five centuries, but";
          
          // Check if there is a url in the text
          if(preg_match($reg_exUrl, $text, $url)) {
          
          
                 return preg_replace($reg_exUrl, $url[0], $text);
          
          } 
          
          }
          

          【讨论】: