特殊的Ascii字符在正则表达式PHP preg_match()中转换为HTML实体

There is a simple regular expression which replaces semicolon from string with "*"

preg_replace('/\;/', '*', $string);

But if I use "♠♣♥♦" characters as string it converts it to HTML code: ♠♣♥♦ and then replace ";" with "*", so output is ♠*♣*♥*♦*

How can I stop this characters from being converting to HTML codes?

You can skip html entities with this kind of pattern:

$result = preg_replace('~&(?:#[0-9]+|[a-zA-Z]+);(*SKIP)(*FAIL)|;~', '*', $text);

&(?:#[0-9]+|[a-zA-Z]+); describes html entities.

The (*SKIP) verb forces the substring matched on the left to not be retried (to be skipped) if the pattern fails later. (*FAIL) forces the pattern to fail.

In this way the second alternative (so ;) is never a part of an html entity.

An other possible way consists to convert all html entities before, to perform the replacement with strtr($text, ';', '*') and to convert special characters to html entities again.