0

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 ?

  • Why not replace the dot with an = and use the $_GET variable? – Daan Nov 20 '17 at 15:30
  • @Daan How do you proceed for do that ? I'm beginner with PHP. – Alessio Cammarata Nov 20 '17 at 15:31
  • Create the URL like this: http://test.com?param=6, In your PHP code use `echo $_GET['param']` – Daan Nov 20 '17 at 15:32
  • I'm confused with your descriptions of code instead of real code. If you mean that you have `.6` and want to strip the first character, good old `substr()` should do. – Álvaro González Nov 20 '17 at 15:37
  • Was this URL format your choice? Having a key with no value in a query-string isn't unheard of, but it does sound like it's now causing problems. As suggested, if you can change the format, then do. If not, you can use any number of string functions to resolve this (`explode`, `trim`, `substr`, `preg_match`, etc). The best one for the job will depend on what other forms the string can take. – iainn Nov 20 '17 at 15:38
  • @chris85 thanks, it works ! And thanks everyone for your help ! – Alessio Cammarata Nov 20 '17 at 15:42
  • Great, I've moved that comment to an answer. Please accept if you've tested and that resolves the issue. – chris85 Nov 20 '17 at 16:01

3 Answers3

0

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+]/'

Kristiyan
  • 1,655
  • 14
  • 17
0

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.

David J Eddy
  • 1,999
  • 1
  • 19
  • 37
0

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)

chris85
  • 23,846
  • 7
  • 34
  • 51