0

i have some ip log i want to return how you see on example some are random and some others are very long 599999999999999999.599999999999999999.599999999999999999

Example:

"192.168.0.1" => "192.168"
"1.3.4.5.6.9" => "1.3"
"50005.60005.60001.404" => "50005.60005"
"192.1.2.5" => "192.1"
etc.
  • 1
    could be done with `explode()` and those are not all IP's –  Oct 03 '18 at 20:30
  • sorry but i have to ask how to use `explode()` i am new student at php – game_over_new Oct 03 '18 at 20:31
  • `$array = explode('.', $yourString);`. That will make an array with each number between the dots. After that you can use `echo $array[0] . '.' . $array[1];` to output the first number, a dot, and the second number – icecub Oct 03 '18 at 20:33
  • Possible duplicate of [How can I split a comma delimited string into an array in PHP?](https://stackoverflow.com/questions/1125730/how-can-i-split-a-comma-delimited-string-into-an-array-in-php) – icecub Oct 03 '18 at 20:34

1 Answers1

1
$ip = '192.168.0.1';

$e = explode('.', $ip); // Split on the dot

echo $e[0] . '.' . $e[1]; // Glue back the first 2 pieces

explode() splits the string into an array using the delimiter (the dot in this case). The array will contain each number (or string) between the delimiter. After that, you can build a new string with that array by concatenating the array keys you need.

Keep in mind that the delimiter will be "lost" after using explode(). So you'll have to add that yourself again.

Reference: http://php.net/manual/en/function.explode.php

Demo: https://ideone.com/1TxtQy

icecub
  • 8,615
  • 6
  • 41
  • 70
  • 1
    You should at the very least explain how your answer works. Just giving "copy / paste" code will solve the issue, but doesn't teach OP anything. He'll be back again later on. – icecub Oct 03 '18 at 20:38
  • @icecub i though the text notes and links did that, what would you like (me (or you can)) to add? –  Oct 03 '18 at 20:38
  • Check my comment to the question. It explains (in short) what's happening. I would add something like that. – icecub Oct 03 '18 at 20:39
  • i see it , my others codes were on conflict with **$ip** becuase i use `$ip = array();` now i see how to do it, this did the job thanks – game_over_new Oct 03 '18 at 20:40
  • @game_over_new If an answer helped you out, please click on the checkmark next to it. This will make sure your question doesn't remain open forever and shows your appreciation to the person that helped you out :) – icecub Oct 03 '18 at 20:42