开发者

Question on PHP Regex [closed]

开发者 https://www.devze.com 2023-04-05 15:34 出处:网络
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. 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

精彩评论

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

关注公众号