How do I strip out this php string?

Glenn

Member
If I have the string "somepage/otherpage/" set to $newUrl, how do I get "otherpage" set to $secondPage? Here is what I have.

$newUrl = $_GET['newUrl'];

preg_match("/^[A-Za-z0-9' -_ ]*(?=\/)/", $newUrl, $matches);
$firstPage = $matches[0];
echo $firstPage, "<br>";

preg_match("/(?<=\/)[A-Za-z0-9().&-', -_ ]*(?=\/)/", $newUrl, $matches);
$secondPage = $matches[0];
echo $secondPage, "<br><br>";

$firstPage is working, $secondPage isn't.

I need it to work with "somepage/otherpage/" and "somepage/otherpage"
 
Last edited:

Glenn

Member
I now have this:

$start = 0;
$end = strpos($newUrl, "/") ;

$firstPage = substr($newUrl, $start, $end-$start);
echo $firstPage, "<br>";


$start = strpos($newUrl, "/") + 1;
$end = 256;

$secondPage = substr($newUrl, $start, $end-$start);
$secondPage = str_replace("/", "", $secondPage);


echo $secondPage, "<br><br>";


And I think it is going to work. Thanks for the input.
 
Last edited:

chrishirst

Well-Known Member
Staff member
Okay, so why are you using regular expressions when all a URL type string needs is a

PHP:
 $parts = explode($urlstring,'/');

decode it.
 
Top