0

I have something saved in my database like this

 PHP,HTML,CSS,SQL,JQUERY,.... 

More can stile show or it might end up being 2 or 1 depending on how may saved for current viewing post.

Now i need to Output each string separately see below example

echo $str1; Output = PHP,

echo $str2; Output = HTML,

echo $str3; Output = CSS,

echo $str4; Output = JQUERY,

I tried using this but i don't understand what is showing me please i need help with it

Here is my code

<?php
$str = 'one,two,three,four';

print_r(explode(',',$str,0));

print_r(explode(',',$str,2));

print_r(explode(',',$str,-1));
?> 

And the out put is this, is not what i want

Array ( [0] => one,two,three,four )
Array ( [0] => one [1] => two,three,four )
Array ( [0] => one [1] => two [2] => three ) 
2
  • 1
    uh, why not just have $arr = explode(',', $str)? The default for explode is to explode on ALL matching elements. you only specify the 3rd argument if you want LESS than than 'all'. Commented Jun 6, 2016 at 14:41
  • Why not just echo the indexes? You also might consider php.net/manual/en/function.str-getcsv.php, rather than explode. Commented Jun 6, 2016 at 14:42

4 Answers 4

4

maybe try this:

$str = 'one,two,three,four'; 
$newstr = explode(',',$str);

$n = 'str'; //sufix
$c=1; //counter
foreach($newstr as $value){
    $nn = $n.$c; //assign vars
    $$nn = $value; //assign value for var
    $c++;
}

var_dump($str1);
var_dump($str2);
var_dump($str3);
var_dump($str4);

response:

string 'one' (length=3)    
string 'two' (length=3)    
string 'three' (length=5)  
string 'four' (length=4)
Sign up to request clarification or add additional context in comments.

Comments

1

Use array!

<?php
$str = 'one,two,three,four';

$array= explode(',',$str);

//Print each array element 
//('one' 'two' 'three' 'four' an so on...)
foreach($array as $element){
    echo $element;
}
?>  

3 Comments

This answer is too simple
This answer does not solve the has more or can be 1 or 2. You will receive either not all of your results or an out of index errors. You should be looping. Not even saying my answer although I have not tested it @miglio answer looks to be the best.
@nerdlyist yup, i know. I just give him a way to know array. miglio answer is the best answer to fit his question
1

This is correct explode creates an array and you need to loop the elements. (you also do not really need the max int.)

<?php
$str = 'one,two,three,four';
$arr = explode(',',$str);
foreach($arr as $elem){
    echo $elem;
}

Comments

0

This will help I think

// Example
$str = 'one,two,three,four';
$pieces = explode(',', $str);
echo $pieces[0]; // one
echo $pieces[1]; // two
echo $pieces[2]; // three
echo $pieces[3]; // four

//Example 2
$str = 'one,two,three,four';
list($one, $two, $three, $four) = explode(',', $str);
echo $one; // one
echo $two; // two
echo $three; // three
echo $four; // four

More info Explode PHP.net

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.