1

On PHP question: From string "192.168.0.23, 192.168.2.33, 124.125.126.127" remove IP address ", 124.125.126.127". leave only: "192.168.0.23, 192.168.2.33"

Thank you.

2 Answers2

1

Use this regex: /[\w.]{15}/ and replace with nothing or empty string "".

<?php
$string = '192.168.0.23, 192.168.2.33, 124.125.126.127';
$pattern = '/[\d.]{15}/';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
?>

Then, remove the last comma with some native php string function. (I don't know much about php)

Demo: http://ideone.com/xNCVNC

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
0

A regex free solution

<?php
$str='192.168.0.23, 192.168.2.33, 124.125.126.192';
print_r(array_filter(explode(', ',$str),function ($v){  return substr($v,0,3)=='192'?$v:''; }));

Working Demo

OUTPUT :

Array
(
    [0] => 192.168.0.23
    [1] => 192.168.2.33
)
Community
  • 1
  • 1
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126