Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
开发者_C百科 Improve this questionI 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";
}
?>
精彩评论