Strip numbers but only if they're not independent
Posted: Tue Mar 23, 2010 8:03 am
Independent meaning numbers after special characters like a dash (e.g. something-1234), a dot (e.g. 1.2.3.4), a ":" (e.g. 11:15) a or just standalone numbers (i.e. something 1234), but not something1234.
I have a function that cleans a search term before searching it (so you wouldn't search for commas, question marks, etc.). But how do I make it remove numbers only if they're not independent? I believe that's what Google somehow does.
Thanks!
I have a function that cleans a search term before searching it (so you wouldn't search for commas, question marks, etc.). But how do I make it remove numbers only if they're not independent? I believe that's what Google somehow does.
Code: Select all
function cleanhouse($str) {
$str = preg_replace('/[\d\\\?\=\+\&\`\~\!\@\#\$\%\^\*\(\)\; \,\.\/\_]/', ' ',$str); // Leaving just words, but missing independent numbers
$str = trim(preg_replace('/\s\s+/', ' ', $str)); // Cleaning space leftovers from above - would it be faster to run the trim first?
return $str;
}
echo cleanhouse("bla, what's up?"); // bla what's up (good) - BTW, I kept the single quote to avoid the bad search term "what s up"
echo cleanhouse('bla?!'); // bla (good)
echo cleanhouse('bla 1'); // bla (good)
echo cleanhouse('bla1'); // bla (bad)
echo cleanhouse('bla-1'); // bla (bad)