开发者

Check string for substring with "Wild Character"

Is开发者_StackOverflow中文版 it possible to check a string for a substring plus a wild character?

I am checking a url for a substring and I know there is the substring function but I am wondering if you can use the * character? Below is my function so far.

function press_getUrl(){

$valid = true;
$current_url = $_SERVER['REQUEST_URI'];
//This is where I am checking for a substring plus whatever is after.
if ($current_url == "/search/node/*"){$valid = false;}

return $valid;

}

$pressValid = press_getUrl();


You don't need no wild characters for substring.

function press_getUrl(){
  return (strpos($_SERVER['REQUEST_URI'], "/search/node/") === 0);
}

However, I see no use for such a function at all. what you're trying to get?

It would make sense for me if it was at least

function press_check_url($check){
  return (strpos($_SERVER['REQUEST_URI'], $check) === 0);
}

and then called

if (press_check_url("/search/node/"))...

but I am not sure of the use of this function.

also a note on the name. your function return no urls. so, don't use "get" in it's name.
and don't mix naming conventions.


This code checks if string starts with /search/node/:

if (strpos($current_url, "/search/node/") === 0){$valid = false;}


No, you cannot. Use strpos() or similar for that example.

If you want to do more complex matching (wildcards in the middle of strings, et cetera), you can use regular expressions.


You just search for position of /search/node, if position != 0, this statements fails

if (0 === strpos($current_url, "/search/node/")) return false; else return true;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜