phppreg-replacestrip

How to Strip Slashes and the Numeric Content Between Them in a String


Consider the following string:

/0123456789/abcdefg

I need to remove all the numbers between the two slashes as well as remove the slashes, too. I know about preg_replace, but have never worked with this function (am new to PHP). How would I make a correct regex for it? Thanks in advance.


Solution

  • Simplest way without preg_replace():-

    <?php
    
    $url = "/0123456789/abcdefg";
    echo $words = substr($url, strrpos($url, '/') + 1);
    

    https://3v4l.org/IbG7E

    Note:- strrpos gets the position of the last occurrence of the slash; substr returns everything after that position.

    strrpos function started counting the position of the given character from 0 that's why +1 is added to get the actual position from where the sub-string can be taken out