-1

我如何在这里摆脱行尾的 NULL ?

WYOMING MI NEW JERSEY 07728 电脑维修技术NULL

$phrases = array("NEW YORK NY", "NEW JERSEY", "SOUTH DAKOTA", "SOUTH CAROLINA",     "COMPUTER REPAIR TECH","YORK NY","07728","WYOMING MI","WYOMING MINNESOTA");
$string = ("I live in wyoming Minnesota, but used to live in New Jersey 07728 working     as a computer repair tech.");
$string = strtoupper($string);

$matches = stringSearch($phrases, $string);

var_dump($matches);

function stringSearch($phrases, $string){
    $phrases1 = trim(implode('|', $phrases));
    $phrases1 = str_replace(' ', '\s', $phrases1);

    preg_match_all("/$phrases1/s", $string, $matches);

    $value = implode(' ', array_filter($matches[0]));
    echo $value;

}
4

2 回答 2

2

该函数不返回任何内容。因此,该$matches变量将包含值null。该值由 输出var_dump($matches),就在函数内的字符串被echo编辑之后。

换句话说,它不是结果字符串的一部分,而是单独的输出。删除var_dump($matches)它就消失了。

于 2012-11-13T13:09:37.177 回答
1

var_dump是导致问题的原因!

试试这个!

$phrases = array("NEW YORK NY", "NEW JERSEY", "SOUTH DAKOTA", "SOUTH CAROLINA",     "COMPUTER REPAIR TECH","YORK NY","07728","WYOMING MI","WYOMING MINNESOTA");
$string = ("I live in wyoming Minnesota, but used to live in New Jersey 07728 working     as a computer repair tech.");
$string = strtoupper($string);

$matches = stringSearch($phrases, $string);

//var_dump($matches);  // <---------- comment this out!!!

function stringSearch($phrases, $string){
    $phrases1 = trim(implode('|', $phrases));
    $phrases1 = str_replace(' ', '\s', $phrases1);

    preg_match_all("/$phrases1/s", $string, $matches);

    $value = implode(' ', array_filter($matches[0]));
    if($value){
        echo $value;
    }

}
于 2012-11-13T13:10:11.227 回答