explode return false?

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
niallsc
Forum Newbie
Posts: 7
Joined: Thu Jun 10, 2010 1:09 pm

explode return false?

Post by niallsc »

What happens if the delimiter I use in the explode function isn't found? is there any way to check for the validity of what is returned from this function?
Thanks!
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: explode return false?

Post by andyhoneycutt »

As per php.net: explode()
Returns an array of string s created by splitting the string parameter on boundaries formed by the delimiter.

If delimiter is an empty string (""), explode() will return FALSE. If delimiter contains a value that is not contained in string and a negative limit is used, then an empty array will be returned, otherwise an array containing string will be returned.
You could always use a strpos() to determine if the delimiter exists... it returns false if the 'needle' isn't found in the 'haystack'.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: explode return false?

Post by AbraCadaver »

You could also do something like this:

Code: Select all

if(($array = explode($delimiter, $string)) && !in_array($string, $array))
Now if the string is comprised of only the delimiter you'll get empty array element(s) but you could use preg_split() with the PREG_SPLIT_NO_EMPTY flag instead of explode to overcome that.

I would probably write a wrapper function that tested with strpos() and then exploded, etc.
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.
niallsc
Forum Newbie
Posts: 7
Joined: Thu Jun 10, 2010 1:09 pm

Re: explode return false?

Post by niallsc »

ok cool thanks for the help!
now I am a super newb and I apologize for my ignorance but I was trying to use strpos but what was is happening is that
I am trying to find a word in a list of words so with strpos if i am trying to find "to" it comes back with "toes" and other words like that
what could i do to accomplish this?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: explode return false?

Post by AbraCadaver »

Show what you are trying to do, inputs and what you want the results to be.
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.
niallsc
Forum Newbie
Posts: 7
Joined: Thu Jun 10, 2010 1:09 pm

Re: explode return false?

Post by niallsc »

all i need is a true or false basically if the word was found in the file or not
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: explode return false?

Post by AbraCadaver »

Code: Select all

$word = 'to';
preg_match('#\b' . $word . '\b#', file_get_contents('/path/to/file.txt'));
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.
niallsc
Forum Newbie
Posts: 7
Joined: Thu Jun 10, 2010 1:09 pm

Re: explode return false?

Post by niallsc »

awesome totally worked! just had to rework the files a little then it all went perfectly! thank you for all your help!
Post Reply