How to use preg_replace

吃可爱长大的小学妹 提交于 2019-12-12 06:22:39

问题


I have a String:

    $str= "(and marque:'Mercedes Benz' model:'F Type')";

I want to return string like this:

    $str2= "(and marque:'Mercedes-Benz' model:'F-Type')";

so, I want to replace space with '-' char but only between '' chars.

Should I use preg_replace function?


回答1:


How about:

$str= "(and marque:'Mercedes Benz' model:'F Type')";
$str = preg_replace("/'(\w+)\s+/","'$1-", $str);
echo $str,"\n";

output:

(and marque:'Mercedes-Benz' model:'F-Type')


来源:https://stackoverflow.com/questions/21727284/how-to-use-preg-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!