php remove string: fa fa-phone famouse

被刻印的时光 ゝ 提交于 2020-01-03 05:21:07

问题


I want to remove the strings fa and fa-phone of the string: "fa famouse fa-phone".

I tried:

preg_replace('/fa.+$/','', 'fa famouse fa-phone');

But now it removes all strings with fa.


回答1:


preg_replace('/fa[ \-](phone)?/','','fa famouse fa-phone');

Regex: https://regex101.com/r/bJ3nE7/1


fa (literal)
[ \-] match a space or a hyphen
(phone)? match the literal 'phone' 0 or 1 times




回答2:


So i want to remove the string fa and all the strings a beginning with fa-
But not other strings that contain fa like famouse for example.

Here is a way to do the job:

echo preg_replace('/fa(?:-[-\w]+|\b)/', '', 'fa famouse fa-phone-small fabrics fantastic'),"\n";

Output:

famouse  fabrics  fantastic


来源:https://stackoverflow.com/questions/28751418/php-remove-string-fa-fa-phone-famouse

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