簡體   English   中英

PHP preg_match_all沒有具體數字

[英]php preg_match_all not specific number

我想從一串數字(例如569048004801)中排除一個特定的數字(例如4800)。我正在為此使用php和方法preg_match_all我嘗試過的一些模式示例:

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

如果您只想查看字符串是否包含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的更多信息,請參見此處文檔

如果您只是想從字符串中刪除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.';
}
/([^4])([^8])([^0])([^0])/i

這實際上是說, 不是“ 4800”的四個字符的序列 關。

/([^4800])/i

這實際上是說, 不是'4','8'或'0'的單個字符

假設您要捕獲其中不包含“ 4800”的數字,我想您可能想要

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

這就是說, 首先檢查我們是否不在某個地方查看帶有“ 4800”的數字字符串,並且在這種情況下,捕獲數字字符串 這稱為“負超前斷言”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM