Question

If I have this one long string:

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 , , 

I want to search for ", ," and add a line break directly after each occurrence so my long string looks like this:

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....

I was thinking I could use stripos to record the positions of each ", ," and add it to an array. Then I could use a for loop to traverse through the array and append a '<br>' after the positions of each double comma. Would that work? Cheers!

Was it helpful?

Solution 2

try 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;

OTHER TIPS

Just replace the string with <br> using str_replace:

$result = str_replace(", ,", "<br>", $str);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top