我有以下字符串
someText 50-90% someText
我只想在字符串是否采用这样的格式%
之后添加50
someText 50%-90% someText
我尝试了以下...
preg_replace('/(\d+)\-[\d]+%/','$0%', 'text 30-50% text')
//the output: text 30-50%% text
preg_match_all('/(\d+)\-[\d]+%/', 'text 30-50% text',$x)
/*$x = array(2) {
* [0]=>
* array(1) {
* [0]=>
* string(6) "30-50%"
* }
* [1]=>
* array(1) {
* [0]=>
* string(2) "30"
* }
*}
*/
preg_replace('/(\d+)\-[\d]+%/','$1%', 'text 30-50% text');
//the output: text 30% text