PHP Stripping!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

PHP Stripping!

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: PHP Stripping!

Post by Weirdan »

Code: Select all

list($name) = explode(" ", trim($name));
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP Stripping!

Post by AbraCadaver »

Or:

PHP 5.3:

Code: Select all

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

Code: Select all

$name = substr($name, 0, strpos($name, ' '));
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Re: PHP Stripping!

Post 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.
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Re: PHP Stripping!

Post 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.
Post Reply