脚本在其他服务器上的行为不同

时间:2013-02-27 09:09:00

标签: php regex

我有一个非常简单的脚本,在不同的服务器上表现得有所不同!

这是剧本:

<?php
$list = file_get_contents("list.txt");
$list = str_replace(Array("\r\n", "\r", "\n", "\f"), "\n", $list);
$all = explode("\n\n", $list);


foreach($all as $k  => $v) {

    $b = preg_replace('/\s+/', '^', $v);
    echo $b."<br/><br/>";
     }
?>

它的内容如下:

System:         Avro
Supplier:       ABC Inc

Quantity:       1
Type:             ICD
Key:              PA-658_ao8uY
For Clarity:  PA-658_AO8UY

Quantity:       10
Type:             PSTHG
Key:              tg675_0O09i8
For Clarity:  PA-658_AO8UY

我有3台服务器要测试,我需要它在所有3上都一样。 它们是php 4.3.10,5.2.13&amp; 5.3.5

脚本在PHP 5.2.13上返回 - 这就是我所期待的。

System:^Avro^Supplier:^ABC^Inc

Quantity:^1^Type:^ICD^Key:^PA-658_ao8uY^For^Clarity:^PA-658_AO8UY

Quantity:^10^Type:^PSTHG^Key:^tg675_0O09i8^For^Clarity:^PA-658_AO8UY

然而在4.3.10&amp; 5.3.5我得到:

System:^ ^ ^ ^ ^Avro^Supplier:^ ^ ^ ^ABC^Inc

Quantity:^ ^ ^ ^1^Type:^ ^ ^ ^ ^ ^ ^ICD^Key:^ ^ ^ ^ ^ ^ ^ PA-658_ao8uY^For^Clarity:^ PA-658_AO8UY

Quantity:^ ^ ^ ^10^Type:^ ^ ^ ^ ^ ^ ^PSTHG^Key:^ ^ ^ ^ ^ ^ ^ tg675_0O09i8^For^Clarity:^ PA-658_AO8UY

如何让所有输出都相同?

由于

更新: 我通过改变:

来实现这一目标

$b = preg_replace('/\s+/', '^', $v);

$b = preg_replace('/(\s|\xa0)+/', '^', $v);

2 个答案:

答案 0 :(得分:1)

最好的猜测:您的输入数据采用允许多字节字符的字符编码,这就是将PHP抛给循环。

您可能需要mb_ereg_replace,即多字节版本。

出于调试目的,查看您接收的实际字符可能会有所帮助,例如使用ord查看每个字节。

答案 1 :(得分:0)

我通过改变:

来实现这一目标

$b = preg_replace('/\s+/', '^', $v);

$b = preg_replace('/(\s|\xa0)+/', '^', $v);

相关问题