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


remove all the special character In start of a string using PHP

如何删除所有的特殊字符在开始(第一个字母应该是字母数字)的字符串使用PHP ?请

$String = "+&,Hello+{+ +$world";

删除字符串开头的所有特殊字符后

字符串应该变成"Hello+{+ +$world"

帮助我。

这将取代开头不是字母数字的所有内容:

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

如果你需要在字符串的开头和结尾裁剪非字母数字字符,使用这个:

<?php
$string = "++&5Hello ++f s world6f++&ht6__)  ";
echo preg_replace('/(^([^a-zA-Z0-9])*|([^a-zA-Z0-9])*$)/', '', $string);

尝试使用trim获取更多信息,请参阅此http://php.net/manual/en/function.trim.php

可以使用ltrim http://www.php.net/manual/en/function.ltrim.php

从字符串的末尾移除,可以使用rtrimhttp://www.php.net/manual/en/function.rtrim.php

示例代码

$String = "+&,Hello+{+ +$world";
echo ltrim($String,"&+,");

你可以在ltrim中添加更多字符来删除字符串
<?php
function string_cleaner($result)
{
    $result = strip_tags($result);
    $result = preg_replace('/[^'da-z]/i', ' ', $result);
    $result = preg_replace('/&.+?;/', '', $result); 
    $result = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', ' ', $result);
    $result = preg_replace('|-+|', ' ', $result);
    $result = preg_replace('/_+/', ' ', $result);
    $result = preg_replace('/&#?[a-z0-9]+;/i','',$result);
    $result = preg_replace('/[^%A-Za-z0-9 _-]/', ' ', $result);
    $result = preg_replace('/^'W+|'W+$/', '', $result);
    $result = preg_replace('/'s+/', ' ', $result);
    $result = trim($result, ' ');
    return $result;
}
?>
<?php
echo string_cleaner($content);
?>

try this

   preg_replace('/[^a-zA-Z0-9_ %'[']'.'(')%&-]/s', '', $String);

使用trim -这是一个内置函数:http://php.net/manual/en/function.trim.php

我认为使用ltrim会更有用,因为你想在字符串的开始:http://www.php.net/manual/en/function.ltrim.php