$string = 'pic.jpeg,pic_2.jpeg,pic_3.jpeg,';
How i can return only strings before the character for example like this: Result:
<input type="text" value="pic.jpeg" />
<input type="text" value="pic_2.jpeg" />
<input type="text" value="pic_3.jpeg" />
This code only return first string (how i can use while here?)
$arr = explode(",", $string, 2);
echo $first = $arr[0];
$arr = explode(",", $string);
foreach($arr as $val){
echo $val;
}
Your original code has a limit on explode
which I've removed.
Dont use a limit in the explode.Also check for value is empty in looping as in your case you will get a empty element in the last position of your array after exploding
$string = 'pic.jpeg,pic_2.jpeg,pic_3.jpeg,';
$arr = explode(",", $string);
foreach($arr as $key=>$val){
if(trim($val) != ''){
echo '<input type="text" value="'.$val.'" />';
}
}
You have a comma on your last so you need to trim that before explode
<?php
$string = 'pic.jpeg,pic_2.jpeg,pic_3.jpeg,';
$string=rtrim($string,',');
$arr = explode(",",$string);
foreach($arr as $k=>$v)
{
echo "<input type='text' value=$v />";
}
You can loop through each element in the array and echo its text. Check the code below
$arr = explode(",", $string, 2);
foreach ($arr as &$value) {
echo '<input type="text" value="$value" />';
}