0

My php problem is following:

I have this string: $days="Monday* Tuesday* Wednesday*"

I want to create an array like this: $d[1]="Monday"; $d[2]="Tuesday"; $d[3]="Wednesday"

I sense that the solution passes by explode(*, $days) but I couldn't get further

Thank you for your help :)

1
  • how about dealing to the source of the $days string to make this easer Commented Aug 5, 2013 at 22:21

5 Answers 5

1
$days="Monday* Tuesday* Wednesday*";
$d=explode('* ', $days);

or

$days="Monday* Tuesday* Wednesday*";
$d=explode(' ', str_replace('*','',$days));

for exclude "*" in end of last word.

Sign up to request clarification or add additional context in comments.

Comments

1
explode('* ', trim($days, '*'));

Comments

1

You should use :

explode("* ", $days)

But IMO, I would use a coma like this :

$days  = "Monday,Tuesday,Wednesday";
$days = explode(",", $days);
echo $days[0]; // => Monday
echo $days[1]; // => Tuesday

Comments

0
$days="Monday* Tuesday* Wednesday*";
$exploded = explode('* ',substr($days, 0, -1));
print_r($exploded);

Output:

Array
(
    [0] => Monday
    [1] => Tuesday
    [2] => Wednesday
)

Comments

0
<?php
$days = 'Monday* Tuesday* Wednesday*';
$d = array_filter(explode('* ', $days));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.