PHP遍历某个子字符串的字符串和换行符

时间:2014-02-19 07:43:16

标签: php string traversal stripos

如果我有一个长字符串:

WAITLIST, 91630, ACCY 2001 , 10, Intro Financial Accounting, 3.00, Zou, Y, DUQUES 251,     TR09:35AM   - 10:50AM, 01/13/14   - 04/28/14 , , WAITLIST, 90003, ACCY 2001 , 11, Intro Financial Accounting, 3.00, Zou, Y, DUQUES 254, TR11:10AM - 12:25PM, 01/13/14 - 04/28/14 , , OPEN, 91212, ACCY 2001 , 12, Intro Financial Accounting, 3.00, Zou, Y, 1957 E B12, TR02:20PM - 03:35PM, 01/13/14 - 04/28/14 , , WAITLIST, 90004, ACCY 2002 , 10, Intro Managerial Accounting, 3.00, Linsley, C, FNGR 222, TR11:10AM - 12:25PM, 01/13/14 - 04/28/14 , , 

我想搜索“,,”并在每次出现后直接添加换行符,所以我的长字符串如下所示:

WAITLIST 91630 ACCY 2001 10 Intro Financial Accounting 3.00 Zou, Y DUQUES 251 TR09:35AM - 10:50AM 01/13/14 - 04/28/14
WAITLIST 90003 ACCY 2001 11 Intro Financial Accounting 3.00 Zou, Y DUQUES 254 TR11:10AM - 12:25PM 01/13/14 - 04/28/14 
OPEN 91212 ACCY 2001 12 Intro Financial Accounting 3.00 Zou, Y 1957 E B12 TR02:20PM - 03:35PM 01/13/14 - 04/28/14 

ETC....

我在想我可以使用stripos记录每个“,,”的位置,并将其添加到数组中。然后我可以使用for循环遍历数组并附加'< br>'在每个双逗号的位置之后。那会有用吗?干杯!

2 个答案:

答案 0 :(得分:2)

只需使用str_replace替换<br>字符串:

$result = str_replace(", ,", "<br>", $str);

答案 1 :(得分:1)

尝试str_replace

$str = 'WAITLIST, 91630, ACCY 2001 , 10, Intro Financial Accounting, 3.00, Zou, Y, DUQUES 251,     TR09:35AM   - 10:50AM, 01/13/14   - 04/28/14 , , WAITLIST, 90003, ACCY 2001 , 11, Intro Financial Accounting, 3.00, Zou, Y, DUQUES 254, TR11:10AM - 12:25PM, 01/13/14 - 04/28/14 , , OPEN, 91212, ACCY 2001 , 12, Intro Financial Accounting, 3.00, Zou, Y, 1957 E B12, TR02:20PM - 03:35PM, 01/13/14 - 04/28/14 , , WAITLIST, 90004, ACCY 2002 , 10, Intro Managerial Accounting, 3.00, Linsley, C, FNGR 222, TR11:10AM - 12:25PM, 01/13/14 - 04/28/14 , ,';
$val = str_replace(', ,', '<br />', $str);
echo $val;