Page 1 of 1
explode return false?
Posted: Tue Jun 15, 2010 11:42 am
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!
Re: explode return false?
Posted: Tue Jun 15, 2010 12:14 pm
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'.
Re: explode return false?
Posted: Tue Jun 15, 2010 2:19 pm
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.
Re: explode return false?
Posted: Tue Jul 20, 2010 10:48 am
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?
Re: explode return false?
Posted: Tue Jul 20, 2010 12:39 pm
by AbraCadaver
Show what you are trying to do, inputs and what you want the results to be.
Re: explode return false?
Posted: Wed Jul 21, 2010 1:10 pm
by niallsc
all i need is a true or false basically if the word was found in the file or not
Re: explode return false?
Posted: Wed Jul 21, 2010 1:18 pm
by AbraCadaver
Code: Select all
$word = 'to';
preg_match('#\b' . $word . '\b#', file_get_contents('/path/to/file.txt'));
Re: explode return false?
Posted: Sun Jul 25, 2010 2:20 pm
by niallsc
awesome totally worked! just had to rework the files a little then it all went perfectly! thank you for all your help!