PDA

View Full Version : Apply trim only to words not to numbers


Alex Piotto
08-21-2002, 09:01 AM
Hi people!

This code is proved to filter all values in a string that are 3 or less character long...

$words=explode(" ",$clean_data);
while(list(,$value)=each($words)){
if(strlen(trim($value))>3) $clean_words[]=$value;
}
$keywords = implode(" ",$clean_words);
unset($clean_words);

How can I avoid to apply the trim if the value is a number?

Any help is welcome...
Thanks
Alex :)

mordred
08-21-2002, 11:14 AM
Aussuming that you get the keywords from a web form, I don't understand why the value gets trimmed and why it should not be for numeric data. For me, the trim() function is unnecessary. Try this out:


$words = explode(" ", $clean_data);
while (list(, $value)= each($words)) {
echo "&gt;" . $value . "&lt;<br>";
if (strlen(trim($value)) > 3) {
$clean_words[] = $value;
}
}
$keywords = implode(" ", $clean_words);
unset($clean_words);


I have the slight notion that you rather want something like


if (is_numeric($value) || strlen(trim($value)) > 3) {

Alex Piotto
08-21-2002, 02:04 PM
Thank mordred...
it seems that lately I am giving you a lot of trouble... but I am studying, and a step at a time, and with the help from you and CodingForum, I will get there....

Meanwhile, thanks for the code. The second option was exactly was I was looking for... but, I would like to explain what I am trying to do. A local agency that rents and sales houses asked me to make a website.

So, as I cannot use mySQL (yet) I hade to build the database and related scripts in a different way. With the help of "Puszbaza scripts" I am able to connect to a database (csv style) and perform several operations.

The only thing is driving me crazy is the search engine for the db. With Puszbaza I cannot connect to all the field of the db at one time, to search the keywords inserted in the form by the users.

At first, I thinked to add a field in the db (it is a dumb solution I know) and put there all the other fields, in order to perform the search only in that specific field and return the other fields to read in a table. But this will duplicate the size of the db... apart this, the system works...

So, I needed to understand what to do differently.
What I thinked was: user enter keywords in a form, like "apartament with 5 rooms and three bathrooms, garage and swimming pool", and I want to filter it to avoid to search for "with" or "and" etc.

After filtering, I need to compare this keywords with the contents of the db (wich may also contain "with", "and" etc), so I create an array with the values of the db.

Now that I know how to retrieve the keyword entered by the user and the keywords from the db (I will use the filter for both) is just a question of campare values and print out only the corrispondent values. I have a long way to go, yet... I'll start this part today!

Do you think this is the right way, or....?

Thanks again
Alex





:thumbsup:

mordred
08-21-2002, 02:40 PM
That sounds alltogether quite reasonable to me. Because you are limited to the flatfile quasi-database, you have to take these extra steps to provide a decent funtionality.

In one of my previous posts I showed you a funtion to search the .csv like files for keywords posted by the user. The search was just looking if one of the keywords matched, i.e. it was a simple "OR" search. You might spend a little time with it and adjust it to your needs. It can be easily adapted to search on more than one field - just pass all fields as a concatenated string to the preg_match function.

Apart from that, try to retrieve your database structure in form of arrays. PHPs array handling functions are very mighty, and combined with regular expressions you should be able to construct a search engine like script yourself.

Perhaps you can guide the user in a way to organize his search query before it gets submitted to your script. I think of simple select lists (dropdowns), maybe one for the number of rooms, one for the price, one for the location. Any extras can be entered manually, but you have control over the format of the other data.

Good luck.

Alex Piotto
08-21-2002, 03:06 PM
Thanks again.
You are rigth... infact, as I can connect to the db one fiel at a time I already wrote the search with dropdown menu. And, as you say, I will use the inout form to target better the search. The code you give me before is on my workbench... and the more I study, the more I understand. It will be my base.

Right now, I've already wrote a script with the form and the other components, and I can view both, db contents and form entries in a table, well cleaned and ready to use for the comparison/search.

Thanks for the support
Alex :) :) :)