Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.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/17.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
通过php和正则表达式从文本字符串查找URL?_Php_Regex_Preg Replace_Preg Match All - Fatal编程技术网

通过php和正则表达式从文本字符串查找URL?

通过php和正则表达式从文本字符串查找URL?,php,regex,preg-replace,preg-match-all,Php,Regex,Preg Replace,Preg Match All,我知道问题的标题看起来很重复。但我在这里没有找到一些解决方案 我需要从文本字符串中查找URL: $pattern = '`.*?((http|https)://[\w#$&+,\/:;=?@.-]+)[^\w#$&+,\/:;=?@.-]*?`i'; if (preg_match_all($pattern,$url_string,$matches)) { print_r($matches[1]); } 使用这种模式,我能够找到带有http://

我知道问题的标题看起来很重复。但我在这里没有找到一些解决方案

我需要从文本字符串中查找URL:

$pattern = '`.*?((http|https)://[\w#$&+,\/:;=?@.-]+)[^\w#$&+,\/:;=?@.-]*?`i';

    if (preg_match_all($pattern,$url_string,$matches)) {
        print_r($matches[1]);
    }
使用这种模式,我能够找到带有
http://
https://
的URL,这很正常。但我有用户输入,人们在其中添加url,比如
www.domain.com
甚至
domain.com

因此,我需要首先验证字符串,在这里我可以将
www.domain.com
domain.com
替换为前面的公共协议
http://
。还是我需要想出更好的模式

我不擅长正则表达式,不知道该怎么办

我的想法是首先查找带有
http://
https://
的url,将它们放在一个数组中,然后用文本字符串中的空格(“”)替换这些url,然后使用其他模式。但我不确定该使用什么模式

我正在使用这个
$url\u string=preg\u replace($pattern,,$url\u string)
但如果在两个有效url之间有
http://
https://

如果你能帮忙,那就太好了

为了让事情更清楚:

$pattern = '`.*?((http|https)://[\w#$&+,\/:;=?@.-]+)[^\w#$&+,\/:;=?@.-]*?`i';

    if (preg_match_all($pattern,$url_string,$matches)) {
        print_r($matches[1]);
    }
我需要一个模式或一些其他的方法,我可以找到一个文本中的所有网址。url的示例如下:

  • domain.com
  • www.domain.com
  • 谢谢!
    5.

    我不确定我是否正确理解了您的需求,但您能否使用以下内容:

    preg_match('#^.+?://#', $url);
    

    要查找字符串上是否指定了协议,如果不只是附加
    http://

    ,是否验证来自带有URL字段的表单的用户输入?或者你是在抓取一个页面/文本块来生成一个在其中找到的URL列表?您试图解析的“文本字符串”的完整示例可能会有所帮助。@baraboom:是,来自用户输入文本框。在这里,人们可以像这样输入twitter:twitter.com/user facebook:etc.谢谢!几乎成功了!!仍然需要找到模式
    domain.com
    @Sisir将
    {1}
    替换为
    ,以使http://或www可选。这对我不起作用。我收到一个空的结果<代码>$pattern='#(www.| https?:\/\/){}[a-zA-Z0-9]{2254}.[a-zA-Z0-9]{2,4}(\S*)#i'$计数=预匹配所有($pattern,'http://www.Imaurl.com“,$matches,PREG_-PATTERN_-ORDER)并且将其复制并粘贴到交互式PHP shell中时不会出现错误,我也会得到空白结果。另外,
    {2254}
    限制不支持像
    t.co
    这样的域名,这些域名现在越来越流行。试图编辑答案,但显然编辑必须大于6个字符:-(哦,我认为这与
    me too.com
    之类的域不匹配。
    $pattern = '#(www\.|https?://)?[a-z0-9]+\.[a-z0-9]{2,4}\S*#i';
    preg_match_all($pattern, $str, $matches, PREG_PATTERN_ORDER);