简体   繁体   English

PHP正则表达式保存以特定字符开头的单词

[英]php regex save word starting withspecific character

though Id figured this out but need your help I need to find words that start with fa- in a string the following works 尽管Id知道了这一点,但需要您的帮助,但我需要在字符串中找到以fa-开头的单词,以下工作

$anchor_css = $item->anchor_css;
if (preg_match_all("/fa\-(\w+)/", $anchor_css, $fa)){   //look for a font awsome class name beginning with fa- if found the lass name is stored in $out[0][0]
    $anchor_css = preg_replace("/fa\-(\w+)/", "", $anchor_css); //remove the font awsome class name starting with fa-
    $anchor_css = preg_replace("/fa/", "", $anchor_css);    //remove the fa prefix from the class
    $anchor_css = preg_replace('!\s+!', ' ', $anchor_css);  //remove multiple spaces and replace with a single space
}
$fa_icon = $fa[0][0] ? '<i class="fa '.implode(' ',$fa[0]).'" ></i>' :'';   //if a font awsome class name exists, create an <i></i> tag

the above works for 'test fa fa-info fa-lg' 以上作品适用于“ test fa fa-info fa-lg”
the result being 结果是
anchor_css='test' fa_icon= 'fa fa-info fa-lg' anchor_css ='test'fa_icon ='fa fa-info fa-lg'
however it does not work for fa-info-circle fa-lg where the results are anchor_css='test' 但是对于结果为anchor_css ='test'的fa-info-circle fa-lg不起作用
fa_icon= 'fa fa-info fa-lg' fa_icon ='fa fa-info fa-lg'

I know it is happening because \\w does not inclue the hyphen but have no idea how to get around this. 我知道它正在发生,因为\\ w不包含连字符,但不知道如何解决。 Help please. 请帮助。

Though your question is not really clear I guess you need somethings like this in line 3 尽管您的问题还不太清楚,但我想您需要在第3行中输入以下内容

$anchor_css = preg_replace("/fa[-_](\w+)/", "", $anchor_css);

If you'd explain more what do you want to achieve then someone could give you better solution. 如果您要解释更多您想要实现的目标,那么有人可以为您提供更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM