关于PHP正则表达式的问题

时间:2011-09-19 09:39:35

标签: php regex

我是PHP正则表达式的新手,我读了http://php.net/manual/en/function.preg-match.php。但找不到修复我的bug的方法。 你能帮我个忙吗? 感谢。

<?php

$myURL = "/something/";  
// If myURL include two or more than two '/' then return Found, Else return Not found two '/'

if (preg_match("/\/?\//", $myURL)) {
    echo "found";
} else {
    echo "not found";
}
?>

3 个答案:

答案 0 :(得分:6)

我建议使用substr_count

$numSlashes = substr_count($text, '/');

晒。

答案 1 :(得分:3)

<?php

$myURL = "/something/";  
// If myURL include two or more than two '/' then return Found, Else return Not found two '/'

if (preg_match("/\/.*\//", $myURL)) {
    echo "found";
} else {
    echo "not found";
}
?>

答案 2 :(得分:0)

如果你想以正则表达式方式去:     

$myURL = "/some//thing/";  


if (array_search("//", preg_match_all("/\/?\//", $myURL))) {
    echo "found";
} else {
    echo "not found";
}
?>
相关问题