如果URl在X之后包含soming,则PHP返回true
我认为答案很简单,但我真的找不到如果URl在X之后包含soming,则PHP返回true,php,Php,我认为答案很简单,但我真的找不到 $url = $_REQUEST['pageUrl'] // for example http://stackoverflow.com/questions/ask/ 如果url包含“/question/ask/”之后的内容,则返回true 这是真的,我只需要url本身是假的,当它在“问题/询问/返回真”之后有一个字符串时,最好的方法是什么 我知道这是错误的,但答案必须接近这一点 if ($_REQUEST['pageUrl'] != "http://stack
$url = $_REQUEST['pageUrl'] // for example http://stackoverflow.com/questions/ask/
如果url包含“/question/ask/”之后的内容,则返回true
这是真的,我只需要url本身是假的,当它在“问题/询问/返回真”之后有一个字符串时,最好的方法是什么
我知道这是错误的,但答案必须接近这一点
if ($_REQUEST['pageUrl'] != "http://stackoverflow.com/questions/ask/%")
您不能这样做。您可以做的是,从URL中删除该内容,然后检查剩余内容的大小:
function checkURL() {
$url = $_REQUEST['pageUrl'];
// "http://stackoverflow.com/questions/ask/"
$url = str_replace("http://stackoverflow.com/questions/ask/", "", $url);
// ""
return (strlen($url) > 0);
// Returns false, if the URL doesn't have anything.
}
您不能这样做。您可以做的是,从URL中删除该内容,然后检查剩余内容的大小:
function checkURL() {
$url = $_REQUEST['pageUrl'];
// "http://stackoverflow.com/questions/ask/"
$url = str_replace("http://stackoverflow.com/questions/ask/", "", $url);
// ""
return (strlen($url) > 0);
// Returns false, if the URL doesn't have anything.
}
}
//或者使用.htaccess方法
/*
*/
//现在在php方面,您仍然可以使用上述方法,但是添加了
$app = isset($_GET['q']) ? explode('/',$_GET['q']) : "";
if(!empty($app))
{
$found = false;
// check for url position
if(isset($app[5]) && !empty($app[5]))
{
$found = true;
}
}
}
//或者使用.htaccess方法
/*
*/
//现在在php方面,您仍然可以使用上述方法,但是添加了
$app = isset($_GET['q']) ? explode('/',$_GET['q']) : "";
if(!empty($app))
{
$found = false;
// check for url position
if(isset($app[5]) && !empty($app[5]))
{
$found = true;
}
}
}
}您只需在URL中搜索文本,然后通过比较URL的长度和找到的文本的结尾来查看后面是否有更多内容:
function urlHasMoreAfter($thisText, $url)
{
$textPosition = strpos($url, $thisText);
if (!$textPosition) {
echo "The text [$thisText] is not in the url";
return false; // you may want to do something different if the text does not exist in url
}
$textLength = strlen($thisText);
$endOfText = $textPosition + $textLength;
$endOfURL = strlen($url);
if ($endOfURL > $endOfText) {
echo "TRUE - There is more text after [$thisText]";
return TRUE;
} else {
echo "FALSE - There is NOT more text after [$thisText]";
return FALSE;
}
}
然后你会得到这些结果:
$url = 'http://www.stackoverflow.com/questions/ask/123';
urlHasMoreAfter('questions/ask', $url); // TRUE
$url = 'http://www.stackoverflow.com/questions/ask';
urlHasMoreAfter('questions/ask', $url); // FALSE
$url = 'http://www.stackoverflow.com/questions/';
urlHasMoreAfter('questions/ask', $url); // FALSE - Text is not in the url
如果你想在指定的文本后得到“某物”是什么,你可以这样做:
$afterText = substr($url, $endOfText + 1);
您只需搜索URL中的文本,然后通过比较URL的长度和找到的文本的结尾,查看后面是否有更多内容:
function urlHasMoreAfter($thisText, $url)
{
$textPosition = strpos($url, $thisText);
if (!$textPosition) {
echo "The text [$thisText] is not in the url";
return false; // you may want to do something different if the text does not exist in url
}
$textLength = strlen($thisText);
$endOfText = $textPosition + $textLength;
$endOfURL = strlen($url);
if ($endOfURL > $endOfText) {
echo "TRUE - There is more text after [$thisText]";
return TRUE;
} else {
echo "FALSE - There is NOT more text after [$thisText]";
return FALSE;
}
}
然后你会得到这些结果:
$url = 'http://www.stackoverflow.com/questions/ask/123';
urlHasMoreAfter('questions/ask', $url); // TRUE
$url = 'http://www.stackoverflow.com/questions/ask';
urlHasMoreAfter('questions/ask', $url); // FALSE
$url = 'http://www.stackoverflow.com/questions/';
urlHasMoreAfter('questions/ask', $url); // FALSE - Text is not in the url
如果你想在指定的文本后得到“某物”是什么,你可以这样做:
$afterText = substr($url, $endOfText + 1);
url本身是假的??是吗?jea我想知道的是,只有当url后面有somting时才返回true,就像我用“%“我想我需要一个占位符或其他东西,如果占位符设置为true,则返回,否则返回false@BurakTopal检查我的答案。如果有效,请点击勾选按钮接受我的回答。url本身是错误的??嗯?jea我想知道,只有当url后面有somting返回true,就像我用“%”尝试的那样,我想我需要一个占位符或其他东西,如果占位符设置为return true,否则返回false@BurakTopal检查我的答案。如果有效,请点击勾选按钮接受我的答案。