Title: Simple PHP question &#8211; how to parse a url string
Last modified: October 28, 2019

---

# Simple PHP question – how to parse a url string

 *  [TWD](https://wordpress.org/support/users/twd/)
 * (@twd)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/simple-php-question-how-to-parse-a-url-string/)
 * $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 6 years, 7 months ago by [TWD](https://wordpress.org/support/users/twd/).

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Dion](https://wordpress.org/support/users/diondesigns/)
 * (@diondesigns)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/simple-php-question-how-to-parse-a-url-string/#post-12072929)
 * 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.
 *  [Jasonian](https://wordpress.org/support/users/jasonian/)
 * (@jasonian)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/simple-php-question-how-to-parse-a-url-string/#post-12073081)
 * 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 6 years, 7 months ago by [Jasonian](https://wordpress.org/support/users/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.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 3 participants
 * Last reply from: [Jasonian](https://wordpress.org/support/users/jasonian/)
 * Last activity: [6 years, 7 months ago](https://wordpress.org/support/topic/simple-php-question-how-to-parse-a-url-string/#post-12073081)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
