使用PHP检查URL中的某些单词
I am trying to write a php code, that checks if there is a certain word in the url and if so - show something.
I am using this one:
$url = parse_url($_SERVER['REQUEST_URI']);
if($url['path'] == '/main-category/') {
echo........
For example I am looking for "main-category". For http://www.site.com/main-category/ the code is working, but if the url contains subcategory http://www.site.com/main-category/sub-category/, it isn't.
How can I make it find /main-category/ no matter if there is something after it, or not?
I read some topics here but didn't figure it out.
我正在尝试编写一个PHP代码,检查网址中是否有某个单词,如果是 - 我正在使用这个: p>
if($ url ['path'] =='/ main-category /'){ echo ........ code> pre>例如 我正在寻找“主要类别”。 对于 http://www.site.com/main-category/ ,代码正常运行, 但如果网址包含子类别 http://www.site.com/main-category/ 子类别/ ,它不是。 p>
我怎样才能找到/主要类别/无论后面是否有什么东西? p>
我在这里阅读了一些主题,但没有想出来。 p> div>
Use strpos()
. Example from the manual:
<?php
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
?>