使用preg_match链接Shortener

时间:2012-10-22 15:03:03

标签: php regex

我的代码是:

if (!preg_match('^http(s)?://(*)?\.mysite.com(\*)^', $url))
{
  echo "<strong>Error</strong>: Invalid mysite.com link or could shorten link";
} 

我得到了:

Warning: preg_match() [function.preg-match]: 
  Compilation failed: nothing to repeat at offset 12

我正在开发一个类似 bit.ly 的链接缩短版,但我只希望它缩短我特定网站的链接。

我需要一些帮助来解决这个错误。

2 个答案:

答案 0 :(得分:5)

问题在于:

if (!preg_match('^http(s)?://(*)?\.mysite.com(\*)^', $url))
                              ^

您已使用*量词,但未指定该量词应用于何处。您可能希望.*代替*

答案 1 :(得分:5)

星号或星号告诉引擎尝试将前一个标记匹配零次或多次。

if (!preg_match('^http(s)?://(*)?\.mysite.com(\*)^', $url))
                              ↑
                       nothing to match

我相信你的正则表达式包含多个错误。我建议你一起去

if (!preg_match('/^https?:\/\/(?:[a-z\d-]+\.)*mysite.com(?:(?=\/)|$)/i', $url))
相关问题