开发者

Question on PHP Regex [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 9 years ago.

开发者_C百科 Improve this question

I am new to PHP regex, and I read the http://php.net/manual/en/function.preg-match.php. but can't find the way to fix my bug. Could you please give me a hand? Thanks.

<?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";
}
?>


I suggest using substr_count.

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

Shai.


<?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";
}
?>


If you want to go by regex way then:

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜