簡體   English   中英

PHP正則表達式提取兩個詞

[英]PHP regex to extract two words

我正在嘗試從字符串中獲取數值。 理想情況下,包含兩個數字的數組。

我正在使用此PHP,但它似乎僅與第一個匹配,而不與第二個匹配。

$str = 'The address has changed from #1216640 to #1218908';

    preg_match_all(
    '/The address has changed from #(.+?) to #(.+?)/s',
    $str,
    $output,
    PREG_SET_ORDER
    );

print_r($output);

我想要的是一個返回1216640和1218908的數組

您可以使用以下內容,只需在字符串中搜索數字后跟#

$string = 'The address has changed from #1216640 to #1218908';
preg_match_all('/#([0-9]+)/', $string, $matches);
print_r($matches[1]);

輸出量

Array
(
    [0] => 1216640
    [1] => 1218908
)

這是您正在尋找的reg ex

/The address has changed from #([0-9]+) to #([0-9]+)/s

請參見下面的屏幕截圖: 在此處輸入圖片說明

暫無
暫無

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

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