2012-11-13 225 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