문제

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;
?>
도움이 되었습니까?

해결책

Use preg_replace:

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

This removes everything starting the slash.

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top