簡體   English   中英

從字符串使用php正則表達式獲取單詞

[英]Get a word using php regular expression from string

這是我的琴弦

905 Lumens/7.2W/3000K/82CRI/L85 50Khrs
1224 Lumens - 9.6W - 3000K
1632 3000K 12.8W 50Khrs
2448 Lumens/3000K/19.2W 150Khrs

現在我只想獲得帶有W的數字的單​​詞,例如: 7.2W9.6W

我想使用正則表達式在變量中獲取它。

我的代碼是: $watt = preg_match("/[\\d]+W/", $string);

當前我的代碼返回數組,但是我只想要7.2W或9.6W或類似的字符串。

請幫忙。

首先建立的匹配項收集在第三個參數$matches

其次-您需要在正則表達式中考慮一個點( . ):

preg_match("/(\d+\.\d+W)/", $string, $matches);
print_r($matches);
echo $matches[0];   // full match found

如果. 是可選的-然后:

preg_match("/(\d+(\.{0,1})\d+W)/", $string, $matches);
print_r($matches);
echo $matches[0];   // full match found

暫無
暫無

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

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