473,399 Members | 4,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Breaking up a string into individual words

Hi there,

I have a text box that I will use to search a database. I would like
to use it so that it will not use a whole string (ie. 'red striped
top') but instead break it up into individual words (ie. 'red',
'striped', 'top') and maybe put it into and array. I will then use the
words in the array to search the database.

How do I break a string up this way?

Also, is this the most common way of creating a means of searching a
database?

Cheers

Burnsy
Jul 17 '05 #1
4 8729
mr_burns <bi******@yahoo.co.uk> wrote:
Hi there,

I have a text box that I will use to search a database. I would like
to use it so that it will not use a whole string (ie. 'red striped
top') but instead break it up into individual words (ie. 'red',
'striped', 'top') and maybe put it into and array. I will then use the
words in the array to search the database.

How do I break a string up this way?
Read the Fine Manual, online at
http://nl2.php.net/manual/en/ref.strings.php
search for split. But maybe you should look at the PCRE functions...
Also, is this the most common way of creating a means of searching a
database?


Don't think so, but depends on what you actually are searching for. "Full
text searches" might be what you are looking for.
Jul 17 '05 #2
mr_burns wrote:
How do I break a string up this way?
$array = explode(" ", $string_with_spaces);
Also, is this the most common way of creating a means of searching a
database?


For basic search engines, it probably is. However, you will loose the
relationship between the words, which is most of the times is implied.

When you enter "red striped top" in a search engine, you would expect to get
the matches with all the words at the top.

This won't happen when you search the database word by word.

A better way is to use a like clause, which can be build quite easily from a
string. Here's an example:

<?php

$s = "red striped top";
$q = "select * from table where";

$array = explode(" ", $s);
$popped = array();
$like = "";

while (count($array) > 1) {
$clause = implode("%", $array);
if ($like) $like .= " or";
$like .= " field like '%$clause%'";
$popped[] = array_pop($array);
}

$q .=
$like .
($like ? " or field like '%" : " field like '%") .
implode(
"%' or field like '%",
array_merge($array, $popped)
) .
"%'"
;

print "<code>$q</code>";

?>
JW

Jul 17 '05 #3
Fulltext search is the best way to get your work done if you are using
MySQL.

Read these articles on fulltext search:
http://dev.mysql.com/doc/mysql/en/Fulltext_Search.html
http://www.zend.com/zend/tut/tutorial-ferrara1.php

Best regards,
Aditya

Jul 17 '05 #4
Fulltext search is the best way to get your work done if you are using
MySQL.

Read these articles on fulltext search:
http://dev.mysql.com/doc/mysql/en/Fulltext_Search.html
http://www.zend.com/zend/tut/tutorial-ferrara1.php

Best regards,
Aditya

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: derrick | last post by:
I used to program text RPG's in C back in high school, but I was very crude at it and only learned what I needed to get the job done. I am now about to graduate college with a degree in English and...
27
by: The Bicycling Guitarist | last post by:
Hi. I found the following when trying to learn if there is such a thing as a non-breaking hyphen. Apparently Unicode has a ‑ but that is not well-supported, especially in older browsers. Somebody...
12
by: Hp | last post by:
Hi All, Thanks a lot for all your replies. My requirement is as follows: I need to read a text file, eliminate certain special characters(like ! , - = + ), and then convert it to lower case and...
4
by: J | last post by:
anybody know a vb.net command to populate an array with each word in a string? for example " hi how are you" array(0) = hi array(1) = how array(2) = are array(3) = you i know i could...
7
by: Brian Mitchell | last post by:
Is there an easy way to pull a date/time stamp from a string? The DateTime stamp is located in different parts of each string and the DateTime stamp could be in different formats (mm/dd/yy or...
150
by: tony | last post by:
If you have any PHP scripts which will not work in the current releases due to breaks in backwards compatibility then take a look at http://www.tonymarston.net/php-mysql/bc-is-everything.html and...
3
by: Ramper | last post by:
I need to Write a function that will, given an input string containing many words, split that string into individual words. For each word, the function should output the word, its starting index in...
7
by: Leon | last post by:
Hi, I'm creating a python script that can take a string and print it to the screen as a simple multi-columned block of mono-spaced, unhyphenated text based on a specified character width and...
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.