且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用PHP删除字符串开头的所有特殊字符

更新时间:2022-03-19 23:13:55

这将替换开头的所有非字母数字:

This will replace everything in the beginning which is not alphanumeric:

preg_replace('/^([^a-zA-Z0-9])*/', '', $string);

更新:

如果您需要修剪字符串开头和结尾的非字母数字字符,请使用:

If you need to trim non-alphanumeric characters both in the start and in the end of a string use this:

<?php

$string = "++&5Hello ++f s world6f++&ht6__)  ";

echo preg_replace('/(^([^a-zA-Z0-9])*|([^a-zA-Z0-9])*$)/', '', $string);