Search code examples
phpurlpreg-replacepreg-match

Need help remove part from url


I need to get part from url, i know lots tutorial in this website but i'm still unable to get it...

this sample url :

https://www.domain.com/want-to-keep-35/?idku=rbxbbgh3dKqn

I only need this part only from that url, without domain, slash and end url

want-to-keep-35

already try this code :

preg_replace('|https?://www\.[a-z\.0-9]+|i', '', $serp);

but not working. let me know if anyone in here can help me and thanks


Solution

  • If it's that simple, you may use the following simple approach to get what you want:

    $url = 'https://www.domain.com/want-to-keep-35/?idku=rbxbbgh3dKqn';
    $re = '#https://www\..*?\..*?/(.*?)/#i';
    
    preg_match($re, $url, $matches);
    echo $matches[1]; // want-to-keep-35