使用PHP删除字符串开头的所有特殊字符 [英] remove all the special character In start of a string using PHP
本文介绍了使用PHP删除字符串开头的所有特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 PHP 删除字符串开头(首字母应为字母数字)中的所有特殊字符?请
How to remove all the special character In start(First Letter Should Be Alphanumeric) of a string using PHP ? Please
$String = "+&,Hello+{+ +$world";
$String = "+&,Hello+{+ +$world";
删除字符串开头的所有特殊字符后
After remove all special character in start of a string
字符串应该变成Hello+{+ +$world"
The string should become "Hello+{+ +$world"
帮帮我.
推荐答案
这将替换开头的所有非字母数字:
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);
这篇关于使用PHP删除字符串开头的所有特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文