0

I want to exclude a specific number like 4800 from a string of numbers like 569048004801. I'm using php for this and the method preg_match_all some pattern's examples I have tried :

/([^4])([^8])([^0])([^0])/i
/([^4800])/i
4

3 回答 3

3

如果只想查看一个字符串是否包含 4800,则不需要正则表达式:

<?php

$string = '569048004801';

if(strpos($string,'4800') === false){
  echo '4800 was not found in the string';
}
else{
  echo '4800 was found in the string'; 
}

此处文档中有关 strpos 的更多信息

于 2012-08-23T15:56:03.020 回答
2

如果你的意思是你只是想4800从一个字符串中删除,这更容易使用str_replace

$str = '569048004801';
$str = str_replace('4800', '', $str);

另一方面,如果你的意思是你想知道一个特定的数字字符串是否包含4800,这将为你测试:

$str = '569048004801';

if (preg_match_all('/4800/', $str) > 0) {
    echo 'String contains 4800.';
} else {
    echo 'String does not contain 4800.';
}
于 2012-08-23T15:50:02.373 回答
1
/([^4])([^8])([^0])([^0])/i

这实际上是说,不是 "4800" 的四个字符序列。关。

/([^4800])/i

这实际上是说,不是 '4'、'8' 或 '0' 的单个字符

假设你的意思是捕获一个不包含“4800”的数字,我想你可能想要

/(?!\d*4800)\d+/i

这就是说,首先检查我们没有在某处查看带有“4800”的数字字符串,如果是这种情况,请捕获数字字符串。它被称为“负前瞻断言”。

于 2012-08-23T15:53:15.040 回答