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!
explode return false?
Moderator: General Moderators
- andyhoneycutt
- Forum Contributor
- Posts: 468
- Joined: Wed Aug 27, 2008 10:02 am
- Location: Idaho Falls
Re: explode return false?
As per php.net: explode()
You could always use a strpos() to determine if the delimiter exists... it returns false if the 'needle' isn't found in the 'haystack'.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.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: explode return false?
You could also do something like this:
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.
Code: Select all
if(($array = explode($delimiter, $string)) && !in_array($string, $array))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.
Re: explode return false?
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?
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?
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: explode return false?
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.
Re: explode return false?
all i need is a true or false basically if the word was found in the file or not
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: explode return false?
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.
Re: explode return false?
awesome totally worked! just had to rework the files a little then it all went perfectly! thank you for all your help!