简体   繁体   中英

strstr with strpos not working (PHP)

I am trying to trim the end of my URL after certain characters. I have my URL in a SESSION variable.

if(strpos($_SESSION['LP'],'ordrer'))
         {
            $Order1 = $_SESSION['LP'];
            $Order2 = strpos($Order1, "ordrer");
            $Order = substr($Order1,0,$Order2);
            //$Order1 = $Order1(explode('ordrer', $Order1);

         }

I'm first putting my URL in variable. Then looking if there is "ordrer" in it with strpos and then cut the end of my URL after "ordrer". Bassicly I have /Dossiers.php?ordrer=Numero or /Dossiers.php?ordrer=Status and I want to have /Dossiers.php? But I does not do anything to the URL, why is that? Also tried explode(), same result. Thanks

I can't see anything wrong with your code - $Order should contain your URL. I've tested the following code where I put your URL into $url. Could it be that $_SESSION['LP'] is empty?

I've also created a second version at the bottom for you, using explode.

$url = "/Dossiers.php?ordrer=Numero";

//Your version
if (strpos($url,'ordrer'))
     {
        $Order1 = $url;
        $Order2 = strpos($Order1, "ordrer");
        $Order = substr($Order1,0,$Order2);
        echo $Order; // the first part, including ?
     }

//If there is no question mark, you'll just get the whole string
$parts = explode("?", $url);
$Order = $parts[0];
echo $Order; //The first part, excluding ?

Edit: Additional thought - is your problem that "ordrer" should be "order" ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM