jjjhbj111 wrote:
> Hi I have been searching on php.net and getting confused
as to what is best for
> this.
> I have an advanced search which is fine but for my
keywords search box I am
> wanting to remove odd spaces and character placed e.g. a
plus sign or a comma
> etc.
> If I use a preg_replace() I am not sure what to use, I
also read about
> str_replace() and getting confused.
You can use preg_replace to search of an array of regular
expressions
and replace them with a single string. After removing the
unwanted
items, you presumably need to create an array of keywords for
your
search. You also need to get rid of any unwanted spaces. Try
the
following script to see how it can be done ($text is dummy
input from
your search form):
<?php
$unwanted = array('/\+/', '/,/');
$text = ' +this, that,+theOther';
// replace + and commas with a space
$clean = preg_replace($unwanted, ' ', $text);
// use space to create an array from the cleaned-up text
$clean = explode(' ', $clean);
// initialize an empty array
$cleanArray = array();
// loop through the array to reject empty elements
foreach ($clean as $item) {
// empty items return false, so add only if something is
there
if ($item)
array_push($cleanArray, $item);
}
// examine the resulting array
print_r($cleanArray);
?>
--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/