我有以下字符串preg_replace函数只替换(括号)之间
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
当前输出是'文本30-50 %%文本',但我希望它成为'文本30%-50%文本.... ....每个数字后的'%'。 – Hilmi