• $url = ‘/devel/lms/ja/lessons/planning-a-meeting-int/’

    how can I parse this such that:

    $new_url = ‘/lessons/planning-a-meeting-int/’

    I was expecting THIS to work:

    $my_array = explode(“/”,$url,4);
    //uses the optional 3rd parameter of the explode function
    //https://www.geeksforgeeks.org/php-explode-function/

    $new_url = implode(‘/’.$my_array[3]);
    //uses the implode function to add back the preceding slash
    //and convert the array item back to a string

    What am I doing wrong?

    • This topic was modified 5 years, 1 month ago by TWD.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Dion

    (@diondesigns)

    If there is a leading slash, then what you want is this:

    $my_array = explode('/',$url,5);

    There is no need to use implode() because the last element in the array will not be broken up. So in the above case, $my_array[4] will contain the string you desire, minus the leading slash.

    Give this a try:

    $url = "/devel/lms/ja/lessons/planning-a-meeting-int/";
    $my_array = explode("/",$url,5);    
    $new_url $my_array[4];
    

    echo $new_url
    should give you
    lessons/planning-a-meeting-int/
    Just as stated above.

    • This reply was modified 5 years, 1 month ago by Jasonian.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Simple PHP question – how to parse a url string’ is closed to new replies.