PHP search through a string

Questions about programming languages and debugging
Post Reply
User avatar
visser
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 472
Joined: 03 Apr 2007, 16:00
17
Location: online
Contact:

PHP search through a string

Post by visser »

Hey all. ive been working with php more and more now for these past couple of months. im really enjoying it. What im trying to do now is create a search function for a website im creating.

basicly it needs to search through a mysql table for a certain word or set of words. im not fully sure how to do that tho. so i have a coujlpe ideas but need more info.

1. with a myslq query like "SELECT (column) FROM (table) WHERE (column) (operator) (value)" if i put a single word in there and the cell in the table itself contains more than a single word. will it search through that cell for that word or look for a cell that specifically has that word and only that word in it?

2. i started thinking farther into it and came across a built in string function that i thought might have some possibilities to it. the function is "substr_count()" lets use an example of the function in use before i ask my question.

Code: Select all

echo substr_count("Hello world. The world is nice","world");
the output would be:

Code: Select all

2
but what if i were to put in "world is" rather than "world" would it go through and find the number of times it finds the string "world is" or would it echo the number of tiems it found "world" plus the number of times it found "is"

3 is there a built in string function that will go through a string and return if it found the search keywords in the string itself or not.

4 is there a built in string function that i can use to split a string such as "hello world" into an array that would look something like this

Code: Select all

array (
0 = "hello"
1 = "world"
)
so basicly just splitting each word into a value in an array. each world having its own index of the array


any help would be awesome guys! (and girls? lol)

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

What you are looking for is...

explode(): http://se.php.net/manual/en/function.explode.php

strstr(): http://se.php.net/manual/en/function.strstr.php

strstr wont count it though. It will stop when it has found what it's looking for.


When it comes to the SQL question.

SELECT username FROM user WHERE username LIKE '%brain%';


This would select the column username from the table user and select all rows where the column username has "brain" in it. For example if we were to query the Suck-o DB, we would get both G-brain and bad_brain.

You can read more about "LIKE" here:

http://www.1keydata.com/sql/sqllike.html
"The best place to hide a tree, is in a forest"

G-Brain
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 467
Joined: 08 Nov 2007, 17:00
16
Location: NL

Post by G-Brain »

For searching SQL tables, LIKE is likely to be what you want.
I <3 MariaLara more than all of you

User avatar
visser
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 472
Joined: 03 Apr 2007, 16:00
17
Location: online
Contact:

Post by visser »

Cats once again you had the exact answer i was looking for! thanks a bunch

Post Reply