简体   繁体   English

如何使用php将逗号替换为不同的字符串数组值?

[英]How to replace a comma into different string array value using php?

Getting hard time to fix this one, please help me :( 很难解决此问题,请帮助我:(

$string = "Question 1 , Question 2, Question 3"; // Value came from Database;

My code is 我的代码是

while($row = mysqli_fetch_assoc($result)) 
{ $alphas = range('A', 'C');
            $output = str_replace(',', ('<br/>'.$alphas[$row]), $string);
            echo $output
} 

The Result will be like this: 结果将是这样的:

Question 1

Question 2

Question 3

What I want is , every comma will change into different value 我想要的是,每个逗号都会变成不同的值

A. Question 1

B. Question 2

C. Question 3

Please help me guys! 请帮助我! thank you 谢谢

$string = "Question 1 , Question 2, Question 3";

$arr=explode(',',$string);
$alphas = range('A', 'C');

$final=array();


for ($i=0;$i<count($arr);$i++) {

$final[]=$alphas[$i].'. '.trim($arr[$i]).'<br>';

}

echo(join($final));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM