0

I am trying to take a string, remove a list of words and any numbers, and then use the result in a MySQL query.

It will echo out the word I am trying to isolate, but when I place that word equal to the string, it returns false and won't query.

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

So for the string $search="60 main street" echo $remove_odonyms is "60 main" and echo $street_only is "main".

But they won’t query, and they won’t do

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

Edit: The query I am doing is

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

It returns nothing. But when I change it to street_name="main", it returns the result I am looking for.

1
  • Have you any error? looks working code Oct 20, 2014 at 4:49

4 Answers 4

1

You have one extra space so you need to trim to remove space. so before query use trim() to remove space and match exact word in query.

$street_only = trim($street_only);
0

you're leaving you're self wide open for sql injection, btw.... as you can see, there are many ways to do this..

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

?>
0
$textString ="tim timtim lovetim hitim wholetim byetimbye";
$text = preg_replace('/\btim\b/', 'pankaj', $textString);
echo $text; 
0

If you want to filter out a little more, you can do a couple things:

// 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));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.