Page 1 of 1

PHP Stripping!

Posted: Thu Jun 17, 2010 8:24 am
by kdidymus
Good afternoon guys and girls.

I return with another problem after so long away!

I have a simple search engine on my site. You can see it in action at http://www.didymus.org.uk/tree

My PHP code only works on ONE word. If someone selects a Forename search and enters Paul, the code will return two matches. One for me (my middle name is Paul) and one for my Dad (whose first name is Paul).

But some people are entering things like Kristian Paul on a forename search and although this IS my name, because my search engine works only on one word, no results are returned.

So, enough babble. This is what I need to do.

I want my query.php to strip any spaces AND the characters that follow the spaces leaving just ONE word to search. For example if someone searches for:

Kristian Paul

the code accepts the word Kristian but deletes the space and the word Paul leaving..

Kristian

Sounds simple but I have no idea how to implement this. Have tried the PHP manual but to no avail!

Many thanks in advance.

KD.

Re: PHP Stripping!

Posted: Thu Jun 17, 2010 8:45 am
by Weirdan

Code: Select all

list($name) = explode(" ", trim($name));

Re: PHP Stripping!

Posted: Thu Jun 17, 2010 9:54 am
by AbraCadaver
Or:

PHP 5.3:

Code: Select all

$name = strstr($name, ' ', true);
If not:

Code: Select all

$name = substr($name, 0, strpos($name, ' '));

Re: PHP Stripping!

Posted: Thu Jun 17, 2010 10:01 am
by kdidymus
Weirdan.

Your response was awesomely quick and it worked a treat. Thank you. I was so far off that solution it wasn't even funny!

Thank you again.

KD.

Re: PHP Stripping!

Posted: Thu Jun 17, 2010 10:02 am
by kdidymus
AbraCadaver.

Nice name. Nice solution. I used list in the end but you managed to achieve what I was trying to achieve using the PHP Manual.

I just couldn't get my head around it!

KD.