2012-11-13 226 views
-1

如何在行结尾删除NULL?从字符串末尾删除NULL

怀俄明州MI NEW JERSEY 07728电脑维修TECHNULL

$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; 

} 

回答

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; 
    } 

} 
+0

非常感谢! – Lou

+0

解决了您的问题? – Chris

+0

是的,再次感谢。 – Lou

2

该函数不返回任何东西。因此变量$matches将包含值null。该值由var_dump($matches)输出,紧接在函数内部的字符串echo之后。

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

+0

非常感谢! – Lou