2012-10-22 113 views
1

我的代码是:链接Shortner使用的preg_match

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 

我的链接shortner工作,类似于bit.ly,但我只希望它从我的特定网站缩短链接。

我需要一些帮助,这个错误。

回答

5

星号或星号告诉引擎尝试匹配前一个令牌零次或多次。

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

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

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

为什么它总是那些几乎不可察觉的事情! 谢谢你们现在正在工作! – Gromstone

+0

为什么你会忽略字符串末尾的标记?这可能会导致潜在的攻击! –

+0

@JanDvorak - 我相信OP想检查'$ url'是否包含域名'mysite.com',所以'$ url'的尾部部分并不重要。 –

5

的问题是在这里:

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

您已经使用了*量词,但您还没有指定什么应该这个量词被应用到。您可能需要.*以代替*

+0

还有一件事:正则表达式是缺少其分隔符。它需要被包裹在一对里面。 –

+0

OP的模式使用'^'作为分隔符 –

+0

@Ωmega,在这种情况下,它缺少正则表达式的开始。 –