开发者

PHP Regex - Append String To Words That Begin with "#' Character

How would I go about using regex to add a static string to all words in another string that begin with the hash character? For example, I have the following two variables:

$stringtoappend = "someuniqueID";
$mystring = ".someClass #someID #someOtherID";

After processing the开发者_JAVA技巧 string, I'd like to create the following string:

".someClass #someIDsomeuniqueID #someOtherIDsomeuniqueID";


Assuming your strings are common CSS names (alphanumeric + dash), use preg_replace() with the following regular expression:

preg_replace('/(#[\w-]+)/', '$1' . $stringtoappend, $mystring);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜