简体   繁体   中英

regular expression get string between :: without any whitespace

I have trouble with regex. I'm coding smile system.And get smile codes from string with regex, for example :laugh: or :haha: . And I want to get string between :: without anything but alpha characters.

<?php
$a="hello my dear :smile: ho r u?";
$a=preg_replace("/:([a-zA-Z]):/","<b>$1</b>",$a);
//or
$a=preg_replace("/:(\w):/","<b>$1</b>",$a);
echo $a;
?>

None of these regexs work. How can I solve this problem?

You have to allow one or more matchings with + quantificator:
This should work:

$a = preg_replace("/:([a-zA-Z]+):/","<b>$1</b>",$a);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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