How do I strip out this php string?

Discussion in 'Programming' started by Glenn, Jan 6, 2014.

to remove all ads.
  1. Glenn

    Glenn Member

    Messages:
    202
    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: Jan 6, 2014
  2. chrishirst

    chrishirst Well-Known Member Staff Member

    Messages:
    2,662
    Where does $entry come from?
     
  3. Glenn

    Glenn Member

    Messages:
    202
    I don't know why I did that. Now it is how I have it.
     
  4. Glenn

    Glenn Member

    Messages:
    202
    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: Jan 6, 2014
  5. chrishirst

    chrishirst Well-Known Member Staff Member

    Messages:
    2,662
    Okay, so why are you using regular expressions when all a URL type string needs is a

    PHP:
     $parts explode($urlstring,'/'); 
    decode it.
     
  6. Glenn

    Glenn Member

    Messages:
    202
    Hummmm, that'll work!!
     

Share This Page