2014-10-20 5 views
0

문자열을 가져와 단어 목록과 숫자를 제거한 다음 결과를 MySQL 쿼리에 사용하려고합니다.전체 단어가 포함 된 str_replace

내가 분리하려고하는 단어를 에코로 표시하지만 해당 단어를 문자열과 동일하게 배치하면 false를 반환하고 쿼리하지 않습니다.

$remove_odonyms=preg_replace("/\b(st|street|rd|road|ln|lane|ter|terrace|ave|avenue|blvd|boulevard|ct|court)\b/", "", $search); 

function remove_numbers($string) { 
$num = array(1,2,3,4,5,6,7,8,9,0); 
return str_replace($num, '', $string); 
} 
$street_only=remove_numbers($remove_odonyms); 

그래서 문자열을 $search="60 main street"echo $remove_odonyms"60 main"echo $street_only"main"입니다.

그러나 그들은 조회하지 않습니다, 그들은

if($street_only="main"){ echo "true";} 

편집하지 않을 것이다 : 내가 뭐하는 거지 쿼리 그것은 아무 것도 반환하지 않습니다

$query=" 
SELECT * FROM column WHERE number='$int' AND odonym='$odonym' AND street_name='$street_only' 
"; 

입니다. 그러나 street_name="main"으로 변경하면 찾고있는 결과를 반환합니다.

+0

오류를 찾으셨습니까? 보이는 코드가 작동하는지 확인하십시오. –

+0

원하는 출력 대 원하는 결과가 명확하지 않습니다. 일어나고있는 일이나 일어나지 않은 일에 대해 더 명확하게 편집 할 수 있습니까? – JakeGould

답변

1

여분의 공간이 하나 있으므로 공간을 제거하기 위해 손질해야합니다. 그래서 쿼리 전에 trim()을 사용하여 공간을 제거하고 쿼리에서 정확한 단어를 찾습니다. 당신이 좀 더 필터링 할 경우

$street_only = trim($street_only); 
0

, 당신은 몇 가지 작업을 수행 할 수 있습니다 : 당신은 당신이 자기 BTW, SQL 주입을위한 활짝 열려있는 떠난다

// Case sensitive string (you may or may not have a upper/lower, but the function is here to see both...) 
$search   = '16 Main Street'; 
// added case insensitive on the chance there are uppers 
$remove_odonyms = preg_replace("/\b(st|street|rd|road|ln|lane|ter|terrace|ave|avenue|blvd|boulevard|ct|court)\b/i", "", $search); 

function remove_numbers($string) { 
     // remove all numbers 
     return trim(preg_replace('/[0-9]/','',$string)); 
    } 
// Presumming you are storing all lower, force lowercase 
// This will echo "main" 
$street_only = strtolower(remove_numbers($remove_odonyms)); 
0
$textString ="tim timtim lovetim hitim wholetim byetimbye"; 
$text = preg_replace('/\btim\b/', 'pankaj', $textString); 
echo $text; 
0

. ... 볼 수 있듯이이 작업을 수행하는 방법은 여러 가지가 있습니다.

<?php 

$str = '120 taco bell bvld. 123'; 

$str = preg_replace('/[^a-zA-z ]/','',$str); 
$str = str_ireplace(array('street','road','rd','ct','court','bvld'),'',$str); 
$str = trim($str); 

$arr = preg_split('/\s/',$str); 

$just_street = join(' ',$arr); 

print $just_street; 

?> 
관련 문제