Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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_Regex_Url_Extract_Preg Match All - Fatal编程技术网

按域名回显字符串中的所有URL?php

按域名回显字符串中的所有URL?php,php,regex,url,extract,preg-match-all,Php,Regex,Url,Extract,Preg Match All,正在尝试按域名提取所有URL/ 任何开始于 http://reports.example.com/report? https://reports.example.com/report? 字符串包含 $string = "http://reports.example.com/report?id=randomtext afdf sadfsdf https://reports.example.com/report?id=randomtext sdfsd sdf afa geadg"; 我想pr

正在尝试按域名提取所有URL/

任何开始于

http://reports.example.com/report?
https://reports.example.com/report? 
字符串包含

$string = "http://reports.example.com/report?id=randomtext  afdf sadfsdf https://reports.example.com/report?id=randomtext sdfsd sdf afa geadg";
我想preg_match_一切都能用吗

$urls = preg_match_all(~http://reports.example.com/reportid=~|https://reports.example.com/report?id=);
我尝试了这个方法,但没有成功,只是让http ID可变(URL以空格结尾以分隔它们)

preg\u match\u all(“/reports.example.com/main(.*)\”/is“,$contents,
元);;
foreach($将[1]匹配为$url)
{
回显$url。“
\n”; }
  • 在PHP中,正则表达式的开头和结尾只有一个分隔符
  • 只需使用
    s
    设置为可选,即可使协议安全或不安全
  • 是一个特殊的字符,当它是文本时应该转义(尽管你很可能很少会遇到一个不带1个字符的URL)
  • 也是一个特殊的字符,具有类似的场景,尽管在这种情况下,您不会得到匹配,因为
    只会使前面的字符/组成为可选的(它不会像
    那样匹配自身)
  • 尝试:

    演示:


    这还假设
    id
    参数只有字母数字字符,如果允许其他人将它们添加到该字符类中。这还假设URL只有
    id
    参数,并且它总是存在的。

    谢谢这件事真的让我很困惑,我认为它太复杂了。好的,这个怎么样我如何从(提供的$contents=)中提取下面所有的carproof URL?您需要使用backticks,这样URL就不会被链接。请尝试修改我提供的正则表达式以及我建议的提示Hi Chris使用了您生成的链接,它返回的只是数组(0){}数组(0){数组(0){}数组(0){数组(0){数组(0){}数组(0){你能在eval.in上发布你的内容并共享链接吗?你有没有收到分隔符
    ~
    错误?我只是想提取谷歌搜索结果描述中的所有reports.carproof.com URL
    preg_match_all("/reports.example.com/main(.*?) \"/is", $contents, 
    $matches);
    foreach ($matches[1] as $url)
    {
     echo $url. "<br />\n";
     }
    
    https?://reports\.example\.com/report\?id=[a-z0-9A-Z]+