Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
提取URL';使用PHP从字符串中删除_Php_Url - Fatal编程技术网

提取URL';使用PHP从字符串中删除

提取URL';使用PHP从字符串中删除,php,url,Php,Url,如何使用PHP识别字符串中的URL并将其存储在数组中 无法使用explode函数如果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?://

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


无法使用
explode
函数如果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>";
试试这个

$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]);
}

$str='|你好,我是一个http://google.fr |你是说:http://google.fr/index.php?id=1&b=6#2310';
$pattern=`.*((http | ftp)://[\w#$&+,\/:;=?@.-]+)[^\w#$&+,\/:;=?@.-]*?'i';
if(preg_match_all($pattern,$str,$matches))
{
打印($matches[1]);
}

它将工作

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

$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]); 
$regex='/https?\:\/\/[^\',]+/i';
preg_match_all($regex,$string,$matches);
回声“;
打印($matches[0]);
希望这对您有用

$urlstring=“您要筛选的文本位于此处。http://google.com, https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/";
Array
(
    [0] => http://google.com
    [1] => https://www.youtube.com/watch?v=K_m7NEDMrV0
    [2] => https://instagram.com/hellow/
)
preg#u match#u all('#\bhtps?://[^,\s()]+(?:\([\w\d]+\)|([^,[:punct:][\s]./)#',$urlstring,$result); 打印($result[0]);
正则表达式是您问题的答案。以对象操纵器的答案为例..它缺少的只是排除“逗号”,因此您可以尝试此代码排除它们,并给出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>"; $arr = explode(",", $match[0][1]);
print_r($match[0][0]); print_r($arr); echo "</pre>";
$string=“要筛选的文本位于此处。http://google.com,
https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/";
preg#u match#u all(“#\bhtps?://[^\s()])+(?:\([\w\d]+\)|([^[:punct:][\s]|/)#,
$string,$match);
echo“”;$arr=explode(“,”,$match[0][1]);
打印($match[0][0]);打印($arr);回显“”;

它在输出数组中应该有3个结果,而不是2个。
http://google.com
https://www.youtube.com/watch?v=K_m7NEDMrV0
https://instagram.com/hellow/
[\w\d]+=[\w]+否,仍然给出2个结果。有3个URL,但只返回2个。您能看到吗?
数组([0]=>http://google.com, [1] => https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/ )
这可能会有帮助吗?你能提供一个正则表达式的示例吗?不,它不适用于我的字符串。
$string=“要筛选的文本在这里。http://google.com, https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/";否,它仍然只提供2个URL。结果应该是3个URL。也许你想通过添加
i
修饰符使其不区分大小写。例如,
。#i'
请注意,一些URL在查询中使用逗号strings@aampudia:非常好的方法。但是,有没有一种简单的方法可以找到没有协议的URL呢?比如:“你想过滤的文本放在这里。www.google.de,www.youtube.com”。@Marco有。。。但这取决于如何接收URL!是否会有协议,但您不想捕获它??或者url没有协议??请注意,url并不总是包含
http
https
,因为它们也可以仅以
/
开头。当url不以逗号分隔时,此查询是“贪婪的”。另请参阅
$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]); 
$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/
)
$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>";