【问题标题】:Extract URL's from a string using PHP [duplicate]使用 PHP 从字符串中提取 URL [重复]
【发布时间】:2016-08-02 12:51:42
【问题描述】:

我们如何使用 PHP 识别字符串中的 URL 并将它们存储在数组中?

如果 URL 包含逗号,则不能使用 explode 函数,它不会给出正确的结果。

【问题讨论】:

  • preg_match_all("/\b((https?):\/\/)?([a-z0-9-.]*)\.([a-z]{2,3})([-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$])/i", $string, $match);用这个

标签: php url


【解决方案1】:

您可以在这里尝试正则表达式:

$string = "The text you want to filter goes here. http://google.com, https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/";

preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $string, $match);

echo "<pre>";
print_r($match[0]); 
echo "</pre>";

这给出了以下输出:

Array
(
  [0] => http://google.com
  [1] => https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/
)

【讨论】:

  • 它应该在输出数组中有 3 个结果。不是 2. http://google.com ,https://www.youtube.com/watch?v=K_m7NEDMrV0https://instagram.com/hellow/
  • [\w\d]+ === [\w]+
【解决方案2】:

试试这个

function getUrls($string)
{
$regex = '/https?\:\/\/[^\" ]+/i';
preg_match_all($regex, $string, $matches);
return ($matches[0]);
}
$urls = getUrls($string);
print_r($urls);

$str = '<a href="http://foobar.com"> | Hello world Im a http://google.fr |     Did you mean:http://google.fr/index.php?id=1&b=6#2310';
$pattern = '`.*?((http|ftp)://[\w#$&+,\/:;=?@.-]+)[^\w#$&+,\/:;=?@.-]*?`i';
if (preg_match_all($pattern,$str,$matches)) 
{
print_r($matches[1]);
}

会有用的

【讨论】:

  • 不,仍然给出 2 个结果。有 3 个 URL,但只返回 2 个。你能看到吗? Array ( [0] =&gt; http://google.com, [1] =&gt; https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/ )
  • 你能用那个正则表达式提供一个例子吗?
  • 不,它不适用于我的字符串。 $string = "The text you want to filter goes here. http://google.com, https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/";
【解决方案3】:

请尝试使用下面的正则表达式

$regex = '/https?\:\/\/[^\",]+/i';
preg_match_all($regex, $string, $matches);
echo "<pre>";
print_r($matches[0]); 

希望这对你有用

【讨论】:

  • 当 url 没有用逗号分隔时,此查询是“贪婪的”。
【解决方案4】:
$urlstring = "The text you want to filter goes here. http://google.com, https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/";

preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $urlstring , $result);

print_r($result[0]); 

【讨论】:

  • 不,它仍然只提供 2 个 URL。结果应该是 3 个 URL。
【解决方案5】:

REGEX 是您问题的答案。以对象操纵器的答案..它所缺少的只是排除“逗号”,因此您可以尝试排除它们并给出 3 个单独的 URL 作为输出的代码:

$string = "The text you want to filter goes here. http://google.com, https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/";

preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $string, $match);

echo "<pre>";
print_r($match[0]); 
echo "</pre>";

输出是

Array
(
    [0] => http://google.com
    [1] => https://www.youtube.com/watch?v=K_m7NEDMrV0
    [2] => https://instagram.com/hellow/
)

【讨论】:

  • 也许您想通过添加 i 修饰符使其不区分大小写。 IE。 ...#i'
  • 请注意,一些 URL 在其查询字符串中使用逗号
  • @aampudia:非常好的方法。但是有没有一种简单的方法来查找没有协议的 url?例如:“您要过滤的文本位于此处。www.google.de, www.youtube.com”。
  • @Marco 有......但这取决于如何接收网址!会有协议,但你不想捕获它?还是网址没有协议??
  • 请注意,网址并不总是包含httphttps,因为它们也可以仅以// 开头。
【解决方案6】:
$string = "The text you want to filter goes here. http://google.com,
https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/";

preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#',
$string, $match);

echo "<pre>"; $arr = explode(",", $match[0][1]);
print_r($match[0][0]); print_r($arr); echo "</pre>";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 2023-02-02
    • 2015-08-03
    • 1970-01-01
    • 2017-04-01
    相关资源
    最近更新 更多