0

这是用于分析字符串...需要分解字符串以获取所有字符。基本上:

$string = "This is (1990-2002) some, text [after] this.";

我需要做什么才能得到这个:

$string = "This is ( 1990 - 2002 ) some , text [ after ] this .";

这可能是这样的:

$string = str_replace('','',$string);

注意:如果插入双空格并删除它,则不是问题...

4

3 回答 3

4

试试这个:

$string = "This is (1990-2002) some, text [after] this.";
$pattern = '/([^a-zA-Z0-9])/';
$replace_pattern = ' $1 ';
echo preg_replace($pattern, $replace_pattern, $string);
于 2012-10-06T23:45:38.737 回答
2
 $string = 'This is (1990-2002) some, text [after] this.';

 $replace = preg_replace("/([^a-zA-Z0-9\s])/", " $1 ", $string);
 // This is ( 1990 - 2002 ) some , text [ after ] this .
于 2012-10-07T00:05:45.347 回答
0
$out = '';
for ($i = 0; $i<strlen($string); $i++) {
    if(ctype_alnum($string[$i])) {
        $out .= $string[$i];
    } else {
        $out .= ' ' . $string[$i] . ' ';
    }
}
// $out contains the fixed string
于 2012-10-06T23:39:11.947 回答