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.
Simplest way without preg_replace()
:-
<?php
$url = "/0123456789/abcdefg";
echo $words = substr($url, strrpos($url, '/') + 1);
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