![](/img/trans.png)
[英]How to get specific number of characters after a string from a string in php
[英]How to get integer from specific text and characters in a string (Regex/PHP)
我一直難以使用preg_replace()
從字符串中提取特定字符。 所有字符串都與下面兩個示例中顯示的一致。
我正在嘗試提取數量 integer,即。 對於第一個例子,我會得到200
,第二個我會得到50
。
示例字符串
$string = 'Sunscreen 25g (200 Quantity)';
$string = 'Lubricant 100ml (50 Quantity)';
正則表達式代碼
$product = preg_replace('/(Sunscreen|Lubricant)/i', '', $string);
其次是:
$product = preg_replace('/(\(d*.Quantity\))/i', '$0', $product)
預期結果
從第一個字符串: int(200)
第二個字符串: int(50)
任何幫助,將不勝感激。 我無法獲得“數量”之前和“(”之后的數字。
您不需要在此任務中拋出多個preg_
調用,只需匹配整個字符串並僅捕獲第一個遇到的數字(
之后的數字。用捕獲的數字替換整個字符串——這樣就沒有臨時數組可以訪問; 字符串輸入直接轉換為所需的 output 字符串。
代碼:(演示)
$strings = [
'Sunscreen 25g (200 Quantity)',
'Lubricant 100ml (50 Quantity)',
'WD-40 100ml (75 Quantity)',
];
foreach ($strings as $string) {
echo preg_replace('~[^(]+\((\d+).*~', '$1', $string) . "\n";
}
Output:
200
50
75
事實上, preg_replace()
可以愉快地處理一個字符串數組。 (演示)
var_export(preg_replace('~[^(]+\((\d+).*~', '$1', $strings));
分解:
[^(]+ #match one or more non-left-parenthesis characters
\( #match literal left parenthesis
( #begin capture group 1
\d+ #match one or more digits
) #end capture group 1
.* #match the remainder of the string
或者,如果您想創建一個包含數量數字的數組(這不太直接,因為必須從生成的數組中提取目標字符串),您可以使用preg_match()
,但絕對沒有理由使用preg_match_all()
。 \K
重新啟動完整的字符串匹配,因此不需要捕獲組。
代碼:(演示)...與上面相同的output
foreach ($strings as $string) {
echo (preg_match('~\(\K\d+~', $string, $match) ? $match[0] : 'no quantity') . "\n";
}
我在How to get a substring between two strings in PHP? 中找到了一個 function? 並修改為只接受 '(' 的最后一次出現,也可以在這里找到How to get the last occurrence of a string?
function getValue($string){
$start = '(';
$end = 'Quantity';
$string = ' ' . $string;
$ini = strrpos($string, $start);
if ($ini == false) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
$product= (int)getValue('Sunscreen 25g (200 Quantity)');
var_dump($product);
您可以使用帶有捕獲組的單個模式來獲得 200 或 50,而不是進行 2 次替換。
然后,您可以使用例如intval將帶有數字的第 1 組轉換為 int。
\b(?:Sunscreen|Lubricant)\h+[^()]*\((\d+)\h+Quantity\)
解釋
\b(?:Sunscreen|Lubricant)
字邊界,然后匹配其中一個選項\h+
匹配 1+ 個水平空白字符[^()]*\(
匹配 0+ 次除(
和)
之外的任何字符(\d+)
捕獲組 1 ,匹配 1+ 個數字(這是你想要的值)\h+Quantity
匹配 1+ 個水平空白字符\)
匹配)
例如
$re = '`\b(?:Sunscreen|Lubricant)\h+[^()]*\((\d+)\h+Quantity\)`';
$str = 'Sunscreen 25g (200 Quantity)
Lubricant 100ml (50 Quantity)';
preg_match_all($re, $str, $matches);
$result = array_map("intval", $matches[1]);
var_dump($result);
Output
array(2) {
[0]=>
int(200)
[1]=>
int(50)
}
您還可以通過匹配數字和單位來使匹配更加具體:
\b(?:Sunscreen|Lubricant)\h+\d+(?:g|ml)\h+\((\d+)\h+Quantity\)
在您的示例中,如果數字后面的字符串不穩定並且會發生變化,您可以使用 \d 和加號從字符中提取所有數字,例如:
$string = 'Sunscreen 25g (200 Quantity)';
preg_match_all('/\d+/', $string, $match);
print_r($match);
結果應該是:
Array
(
[0] => Array
(
[0] => 25
[1] => 200
)
)
但是如果你的字符串是穩定的(防曬霜和潤滑劑)你可以使用這個正則表達式:
$string = 'Sunscreen 25g (200 Quantity)';
preg_match_all('/Sunscreen ([\d\.]*)/i', $string, $match);
print_r($match);
$string = 'Lubricant 100ml (50 Quantity)';
preg_match_all('/Lubricant ([\d\.]*)/i', $string, $match);
print_r($match);
結果應該是:
Array
(
[0] => Array
(
[0] => Sunscreen 25
)
[1] => Array
(
[0] => 25
)
)
Array
(
[0] => Array
(
[0] => Lubricant 100
)
[1] => Array
(
[0] => 100
)
)
或更簡單:
$string = 'Sunscreen 25g (200 Quantity)';
preg_match_all('/([\d\.]*) Quantity/i', $string, $match);
print_r($match);
結果:
Array
(
[0] => Array
(
[0] => 200 Quantity
)
[1] => Array
(
[0] => 200
)
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.