Link to home
Start Free TrialLog in
Avatar of lilyyan
lilyyan

asked on

explode generating an empty element in an array (2)

Hello,
I'm using explode to delimit a string, for example:

case 1: $testString="apple+";
case 2: //$testString = "+apple orange+";
case 3: //$testString = "apple+orange";
$testStringArr = explode("+", $testString);

The output of the size of the three $testStringArr  both are 2, 3 2.

the result of case 3 is fine.

how could i change the code, so that i could get only "apple" incase 1, and get "apple orange" in case 3?

i used :
for ($k=0; $k<sizeof($testStringArr ); $k++){
      if (strlen($testStringArr[$k])==0 ){
       unset($testStringArr[$k]);
   }

---------------------------
This doesn't work for case 2. Is there something I'm missing? Any suggestion ?

Thanks very much for your reply,
lilyyan
ASKER CERTIFIED SOLUTION
Avatar of glcummins
glcummins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lilyyan
lilyyan

ASKER

Hi, thanks for your reply. It works great!
Avatar of lilyyan

ASKER

one more question, after using trim() function, the unset() process is not necessary to be used ?
That is entirely up to you. My preference would be to simply call unset() on the entire array when you are finished using it. However, if the strings that you are storing as elements are quite massive, then you may be better off calling unset() on each element as you finish using it.
Avatar of lilyyan

ASKER

Hi, thanks very much for your reply. Helpful!