PHP strrchr parse int

70 views Asked by At

I've got a problem. This is my PHP code :

$extract = $query;
$extractpoint = strrchr($extract, ".");

So, $extract is a parse_url of my website address. Exemple : http://test.com?param.6

$extract = param.6 and $extractpoint = .6

BUT, I want a solution to have only the 6, without the point.

Can you help me with that ?

3

There are 3 answers

0
chris85 On BEST ANSWER

The easiest solution would be restructuring the URL. I that is not possible though you can use strpos to find the position of your specific character and then use substr to select the characters after it.

$extract = 'param.6';
echo substr($extract, strpos($extract, '.') + 1);

Demo: https://3v4l.org/CudTAG

(The +1 is because it returns the position of the match and you want to be one place past that)

0
Kristiyan On

There are different ways:

  1. Filter only numbers:

    $int = filter_var($extractpoint, FILTER_SANITIZE_NUMBER_INT);

  2. Replace the point

    $int = str_replace('.', '', $extractpoint) //$int = str_replace('param.', '', $extractpoint)

  3. Use regex

    /[0-9+]/'

0
David J Eddy On

strrchr() results the count of the last instance of a character in a string. In order to get the next character add 1 to the count. Then use substr() to extract the next character from the string.