Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

php search box - keywords

Guest
Feb 17, 2007 Feb 17, 2007
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.
Any suggestions are welcome.
I know how to use trim() but I need more than that for this instance.

Thanks for your time....
TOPICS
Server side applications
543
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 18, 2007 Feb 18, 2007
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/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 25, 2007 Feb 25, 2007
Thanks David,
I have been away from the desk for a while - lots of other work came first.

I will have a play with this and see how it works.
Will let you know after today.

Thanks again.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 10, 2007 Mar 10, 2007
Hi David I have tried the above and just a few questions.
I used the php debugger and I kept getting Undefined Index with the SearchString code but now that is fixed.
Just wondering if the first search box results isn't on the same page, using the
print_r($cleanArray);
would I place this on the results page to see. As it is at the moment it still allows me to place spaces, commas etc.
I will show you what I have done. Also should I place this as a function or leave as is? I have a the search box show up again on the results page.
I did notice since I placed this code that the page loads in slower than before.
<?php
if (array_key_exists('Search', $_GET)) {
$SearchString = $_GET['SearchString'];
$unwanted = array('/\+/', '/,/');

// replace + and commas with a space
$clean = preg_replace($unwanted, ' ', $SearchString);

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

This is the search box:
<table width="100%" border="0" cellpadding="2" cellspacing="0" bgcolor="#1F1F1F">
<tr>
<td><input name="SearchString" type="text" class="search" id="SearchString" value="<?php if (isset($_GET['SearchString'])) {
echo htmlentities($_GET['SearchString']);} ?>" size="26" maxlength="26" />
<input name="Search" type="submit" id="Search" style="padding:0;background-color:#000000;border: 1px solid #990000;color:#FFFFFF;width:35px;font-weight:bold" value="Go" /></td>
</tr>
</table>

I haven't supplied the form code - not necessary.
The CSS you see for the button is only for the button, the CSS for everything else is in a style sheet.
Every button has a different size that is why.
<?php echo $_GET['SearchString']; ?> is what is on the search results page.

Thanks for your help.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 11, 2007 Mar 11, 2007
jjjhbj111 wrote:
> Just wondering if the first search box results isn't on the same page, using
> the
> print_r($cleanArray);
> would I place this on the results page to see. As it is at the moment it still
> allows me to place spaces, commas etc.

It needs to be on the page that displays the results.

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 13, 2007 Mar 13, 2007
Thanks David
I did that and what I get is anything that is in $unwanted = array('/\+/', '/,/');
and is placed in the search box I get things like this above my webpage:
Array ( [0] => day [1] => sap )
and it still doesn't remove any plus signs etc. from the users entry into the search box.
I see it is turning it into an array and this is what the print_r($cleanArray); must be outputting. Or is this working and I am not realising it.
I see the Array has separated and removed the plus sign I put in but still shows on the <?php echo $_GET['SearchString']; ?> output on the results page.
If this is the case then if I remove the print_r function, it won't show the array message but still search for the user minus the unwanted signs.

Did I get it???
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 13, 2007 Mar 13, 2007
jjjhbj111 wrote:
> If this is the case then if I remove the print_r function, it won't show the
> array message but still search for the user minus the unwanted signs.

print_r() is simply for testing your results. If print_r() is producing
the search string that you want, you can remove it and just use the
array for your search.

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 13, 2007 Mar 13, 2007
LATEST
Thank you as I was writing it thinking the code wasn't working the light bulb turned on for a second.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines