php remove string: fa fa-phone famouse

2019-06-10 00:27发布

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.

2条回答
\"骚年 ilove
2楼-- · 2019-06-10 00:45

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
查看更多
萌系小妹纸
3楼-- · 2019-06-10 01:10
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

查看更多
登录 后发表回答