通过php和regex从文本字符串中找到URL? [英] Finding urls from text string via php and regex?

查看:233
本文介绍了通过php和regex从文本字符串中找到URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道问题标题看起来很重复.但是我在这里找不到一些解决方案.

I know the question title looks very repetitive. But some of the solution i did not find here.

我需要从文本字符串中找到网址:

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

    if (preg_match_all($pattern,$url_string,$matches)) {
        print_r($matches[1]);
    }

使用这种模式,我能够找到http://https://的网址,这很不错.但是我有用户输入,人们在其中添加www.domain.com甚至domain.com

using this pattern i was able to find urls with http:// and https:// which is okey. But i have user input where people add url like www.domain.com even domain.com

因此,我需要先验证字符串,然后才能在其之前用通用协议http://替换www.domain.com domain.com.还是我需要提出更多好的模式?

So, i need to validate the string first where i can replace www.domain.com domain.com with common protocol http:// before them. Or i need to comeup with more good pattern?

我对正则表达式不好,也不知道该怎么办.

I am not good with regex and don't know what to do.

我的想法是先用http://https://查找网址,然后将它们放入数组,然后用文本字符串中的space(")替换这些网址,然后使用其他模式它.但我不确定要使用哪种模式.

My idea is first finding the urls with http:// and https:// the put them in an array then replace these url with space(" ") in the text string then use other patterns for it. But i am not sure what pattern to use.

我正在使用此$url_string = preg_replace($pattern, ' ', $url_string );,但是如果两个http://https://

I am using this $url_string = preg_replace($pattern, ' ', $url_string ); but that removes if any www.domain.com or domain.com url between two valid url with http:// or https://

如果您能提供帮助,那将是很好的.

If you can help that will be great.

要使事情更清楚:

我需要一种模式或某种其他方法,可以在其中找到文本字符串中的所有网址.网址示例为:

i need a pattern or some other method where i can find all urls in a text sting. the example of url are:

  1. domain.com
  2. www.domain.com
  3. http://www.domain.com
  4. http://domain.com
  5. https://www.domain.com
  6. https://domain.com
  1. domain.com
  2. www.domain.com
  3. http://www.domain.com
  4. http://domain.com
  5. https://www.domain.com
  6. https://domain.com

谢谢! 5.

推荐答案

$pattern = '#(www\.|https?://)?[a-z0-9]+\.[a-z0-9]{2,4}\S*#i';
preg_match_all($pattern, $str, $matches, PREG_PATTERN_ORDER);

这篇关于通过php和regex从文本字符串中找到URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆