Question

I want to get rid of the CIDR notation and tried the following, but it doesn't seem to work like this:

<?php
  $txt='156.67.0.0/16';
  $re='(\\/)'.'(\\d+)';

  $end = rtrim($txt,$re);
  echo $end;
?>
Was it helpful?

Solution

Use preg_replace:

preg_replace('~/.*~', '', $txt);

This removes everything starting the slash.

OTHER TIPS

trim() doesn't accept a regex but a caracter list. You can simply split the string and only use the first part though:

$parts = explode('/', $str);
echo $parts[0];

rtrim accepts a character list, not a regular expression. Look into preg_replace.

$end = preg_replace('@/.*$@', '', $txt);

I would use preg_replace():

$ip = '156.67.0.0/16';
$ip = preg_replace('#/\d+$#', '', $ip);

echo $ip; // 156.67.0.0
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top