【问题标题】:preg_match pattern-matching expression to get URL in phppreg_match 模式匹配表达式在 php 中获取 URL
【发布时间】:2013-08-02 08:08:58
【问题描述】:

我有一个文本如下:
我有这个文本'文本(在括号中)结束'
这里:www.url1.com
在这里:一些文字 (www.url2.com)

我想提取 www.url2.com 并存储在一个变量中。
我正在使用模式:preg_match('/(? 这会导致第 1 行中的括号失败。
提取的字符串为:括号内)
请提供合适的正则表达式来帮助我。

【问题讨论】:

  • 您需要更清楚地指定条件是什么。根据您目前的描述,只能猜测。
  • 我需要一个模式来开始从第二个 URL(www.url2.com) 中提取并跳过第一个 URL (www.url1.com)

标签: php regex url pattern-matching preg-match


【解决方案1】:
preg_match("#\((www\.[^\s]+)\)#", $text, $match);

echo $match[1];

【讨论】:

    【解决方案2】:
    if (preg_match('/\(\s?(www\.\w+\.com)\s?\)/', $text, $matches)){
        $var = $matches[1];
    }
    

    假设.com。如果要允许,例如.net.org,可以修改为:

    if (preg_match('/\(\s?(www\.\w+\.(com|net|org))\s?\)/', $text, $matches)){
        $var = $matches[1];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-01
      • 1970-01-01
      • 2013-06-12
      • 2011-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多