-1

ここの行末のNULLを取り除くにはどうすればよいですか?

ワイオミングMIニュージャージー07728コンピューター修理テクヌル

$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

The function does not return anything. The $matches variable will therefore contain the value null. That value is output by var_dump($matches), right after the string is echoed inside the function.

In other words, it's not part of the result string, those a separate outputs. Remove var_dump($matches) and it's gone.

于 2012-11-13T13:09:37.177 に答える
1

It was the var_dump that was causing the issue!

Give this a try!

$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 に答える