如果UR在X之后包含soming,则PHP返回true

时间:2017-01-04 14:54:03

标签: php

我认为答案很简单,但我真的无法找到答案。

$url = $_REQUEST['pageUrl'] // for example http://stackoverflow.com/questions/ask/

如果网址在此情况下包含某些内容" / question / ask /"返回true 多数民众赞成它我只需要更多的只是网址本身是假的,当它有一个字符串"问题/问/ /返回真的最好的方式是什么?

我知道这是错的,但答案必须接近这个

if ($_REQUEST['pageUrl'] != "http://stackoverflow.com/questions/ask/%")

4 个答案:

答案 0 :(得分:3)

你不能这样做。您可以做的是,从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.
}

答案 1 :(得分:1)

您可以直接搜索网址中的文字,然后通过比较网址长度与找到文字的结尾来查看其后面是否有更多内容:

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);

答案 2 :(得分:0)

// possible way
$url = "http://stackoverflow.com/questions/ask/query=1";//$_REQUEST['pageUrl'];

// check if URL is not empty

if(!empty($url))

{
// ok explode with '/'

$url2 = explode('/', $url);

// get the end

// lets assume we are checking for a view in a method
$found = false;

if(isset($url2[5]) && $url2[5] != "")
{
    $found = true;
}

// this would return true
if($found == true)
{
    // do this
}

}

//或使用.htaccess方法 / *

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?q=$1 [QSA,L] 

* /

//现在在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;
    }
}

答案 3 :(得分:0)

// Another Approach





   $url = "http://stackoverflow.com/questions/ask/"; // OR $_REQUEST['pageUrl'];

  // check if URL is not empty

  function checkUrl($url)
 {
if(!empty($url))
{
    // ok explode with '/'
    $url2 = explode('/', $url);


    // get the end
    // lets assume we are checking for a view in a method

    $found = false;

    $end = end($url2);

    if($end != "")
    {
        // should return the value after X if such is found
        // return true
        return true;
    }
    else
    {
        return false;
    }
}

}