我想從DB
中搜索文本中的數字並打印它們。數字是產品代碼,從4到6位數不等。搜索字符串(變量匹配)並打印
例子:
aaaaa 1234 aaaa
aaaaa 123456 aaaaa
aaaaa 12345 aaaaa
我什麼是5號的精確匹配的數組,但不能變,不打印:
$string = $sql['products_description'];
preg_match_all('/(?<![0-9])[0-9]{5}(?![0-9])/', $string, $match);
for ($i=0; $i<10; $i++) {
echo $match[$i];
echo '<br>';
}
任何幫助嗎?
編輯: 我得到這個緊密合作:
$string = $sql['products_description'];
preg_match_all('#\d{4,6}#', $string, $matches);
foreach ($matches as $match) {
print_r($match);
}
但返回類似:
陣列([0] => 1234 [1] => 123456 [2] => 12345)
我純粹需要它們(不是在一個數組中),因爲我必須稍後分別使用每個數字。在這種情況下,如何提取每個元素並將其打印出來?
SOLUTION:
$var = array();
preg_match_all('#\d{4,6}#', $string, $matches);
foreach ($matches as $match) {
$var[] = $match;
}
for ($i=0; $i<=count($matches[0]); $i++) {
for ($j=0; $j<=count($matches[0]); $j++) {
if (($var[$i][$j]) != '') {
print_r($var[$i][$j]);
echo '<br>';
}
}
}
您需要發佈UR查詢 –
我的查詢是正確的,讓我用數字產品說明裏面 –