如何从 php 中的字符串中删除特殊符号


How to remove special symbol from string in the php

我必须从字符串冒号中删除:例如$chapter=Area of shape:triangle

我已经试过preg_replace('[^a-zA-Z0-9]',$chapter);

您忘记了分隔符和函数preg_replace第二个参数。

preg_replace('~[^a-zA-Z0-9]+~', '', $chapter);

也许我不明白这个问题,或者我过度简化了它,但不仅仅是:

str_replace(":", "", $chapter);

?这将返回$chapter并删除所有冒号。