variation of string::explode() that works?

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
joppe
Forum Newbie
Posts: 7
Joined: Tue Mar 09, 2010 3:18 am

variation of string::explode() that works?

Post by joppe »

$arr = explode("/","//a");

why do i get 2 empty cells before "a" in my array?

$arr[0] == ""
$arr[1] == ""
$arr[2] == "a"
count($arr) == 3

what i'd expect is

$arr[0] == "a"
count($arr) == 0



not sure about the php version, phpinfo is blocked and i have no control over the server - i do know it's oldish
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: variation of string::explode() that works?

Post by s.dot »

do array_filter(explode('/', '//a'));
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
joppe
Forum Newbie
Posts: 7
Joined: Tue Mar 09, 2010 3:18 am

Re: variation of string::explode() that works?

Post by joppe »

that does half the job

i've got an array with count() of 1 like expected, but the value "a" is in cell [2]

i could grab the count()-1 from before the array_filter and use that to point to the value afterwards, but that seems a bit contrived...
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: variation of string::explode() that works?

Post by s.dot »

This is starting to feel ugly, lol... but..

array_values(array_filter(explode('/', '//a')));
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
joppe
Forum Newbie
Posts: 7
Joined: Tue Mar 09, 2010 3:18 am

Re: variation of string::explode() that works?

Post by joppe »

thanks that seems to work... ugly tho' it may be :lol:
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: variation of string::explode() that works?

Post by Weirdan »

Code: Select all

 
$arr = preg_split("#/#","//a", -1, PREG_SPLIT_NO_EMPTY);
 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: variation of string::explode() that works?

Post by AbraCadaver »

Your example is simplistic so I don't know what other inputs you'll have, but strtok() might be of use.
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.
Post Reply