PHP Newb: Dynamic Array

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
thrustinj
Forum Newbie
Posts: 3
Joined: Tue Jun 17, 2003 10:34 am

PHP Newb: Dynamic Array

Post by thrustinj »

I'm trying to take a search string and make an array out of it. I'm better this in ASP.

How do you declare an array or do you even have to?

This is my string convert:
$commentsArray[] = preg_split("", $comments, -1, PREG_SPLIT_DELIM_CAPTURE)

I also want it to split based on spaces and commas.

I just bought for php too...

Thanks for the help
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Could you give us an example of the string that you are trying to explode into an array and an idea of what you need to split it on.

Mac
thrustinj
Forum Newbie
Posts: 3
Joined: Tue Jun 17, 2003 10:34 am

Post by thrustinj »

Well..it's a key word field for a search page. So I have to be ready for spaces, commas...anything people would write to seperate words on. Basically something like:

africa dog nigeria
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post by delorian »

I think you don't need commas, semi-colons, dots etc. in your search engine so the thing to do is to convert those marks to spaces, like:

Code: Select all

ereg_replace(","," ",$string_you_want_to_convert);
// the same with other marks

// it is also good to convert double spaces into one space
Then you use the explode() http://php.net/explode function, which returns an array.

Code: Select all

$your_array = explode(" ",$string_you_want_to_convert);
If you don't change double spaces into one space you will have some empty fields in $your_array.

I think that gave you a hint how to do this yourself.
thrustinj
Forum Newbie
Posts: 3
Joined: Tue Jun 17, 2003 10:34 am

Post by thrustinj »

okay..i think this is working. I'll be back in this thread if anything else comes up related to it.

Thanks!
Post Reply