关于php中的preg_match()的简单问题

时间:2010-08-17 05:55:37

标签: php

我使用下面的代码搜索并查找http是否包含在$ url地址用户输入

if (!preg_match("/http:///", $user_website) 

但是我收到了这个错误

Warning: preg_match() [function.preg-match]: Unknown modifier '/' in

我知道它的http是什么,但是如何工作呢?

7 个答案:

答案 0 :(得分:2)

使用/替代字符来标记模式的开头/结尾,而不是必须遍历URL正则表达式中的每个preg_*

if (!preg_match("#http://#", $user_website)

答案 1 :(得分:2)

你可以像其他答案一样逃避斜线,或者你可以使用不同的分隔符,最好是你的正则表达式中不会使用的字符:

preg_match('~http://~', ...)
preg_match('!http://!', ...)

你真的不需要正则表达式。字符串匹配应该足够了:

if (strpos($user_website, 'http://') !== false) {
    // do something
}

请参阅:strpos()

答案 2 :(得分:2)

您使用/的分隔符也可以在模式中找到。在这种情况下,您可以 转义模式中的分隔符

if (!preg_match("/http:\/\//", $user_website) 

或者您可以 选择其他分隔符 。这样可以保持图案的清洁和简洁:

if (!preg_match("#http://#", $user_website) 

答案 3 :(得分:2)

当然你必须这样做

$parts = parse_url($my_url);

$parts['scheme']将包含url方案(可能是'http')。

答案 4 :(得分:1)

/个字符退出\个字符。

答案 5 :(得分:0)

你需要逃避文字字符。在前斜线前放置一个反斜杠。

http://变为http:\/\/

答案 6 :(得分:0)

if (!preg_match("/http:\/\//", $user_website) 
相关问题