peppericious Posted June 21, 2013 Share Posted June 21, 2013 (edited) I need to mark up a lot of dialogues, which will be created on an ongoing basis. Every second paragraph will be spoken by alternate participants in the dialogue. The dialogues will always be like this, with the participants' names at the start of the paragraphs... <p>Joe: Humpty Dumpty sat on a wall. Humpty Dumpty had a great fall...</p> <p>Jill: The Grand Ol' Duke of York, he had ten thousand men...</p> <p>Joe: And so on and so forth</p> <p>Jill: And whatever the weather</p> But they will need to be marked up like this: <div class='dlg'> <div class='person_1'>Joe</div> <div class='spk_1'>Humpty Dumpty sat on a wall. Humpty Dumpty had a great fall...</div> </div> <div class='dlg'> <div class='person'>Jill</div> <div class='spk'>The Grand Ol' Duke of York, he had ten thousand men...</div> </div> <div class='dlg'> <div class='person_1'>Joe</div> <div class='spk_1'>And so on and so forth</div> </div> <div class='dlg'> <div class='person'>Jill</div> <div class='spk'>And whatever the weather</div> </div> Can anyone suggest a way of doing the conversion? Thanks in advance. Edited June 21, 2013 by peppericious Quote Link to comment https://forums.phpfreaks.com/topic/279420-to-wrap-alternate-paragraphs-in-tags/ Share on other sites More sharing options...
Solution gristoi Posted June 21, 2013 Solution Share Posted June 21, 2013 ok, so lets presume you are outputting the dialogues from an array ( could be db, principle is the same): $i = 0; $messages = array( array('name'=> 'jack', 'message'=>'Lorem Ipsum'), array('name'=> 'jill', 'message'=>'Lorem Ipsum') array('name'=> 'jack', 'message'=>'Lorem Ipsum') array('name'=> 'jill', 'message'=>'Lorem Ipsum') array('name'=> 'jack', 'message'=>'Lorem Ipsum') ); foreach($messages as $message) { if ($i % 2 != 0) # An odd row $person = "person_1"; $spk = "spk_1"; else # An even row $person = "person"; $spk = "spk"; } print '<div class="dlg"> <div class="'.$person.'">'. $message['name'].'</div> <div class="'.$spk.'">'. $message['message'].'</div> </div> '; $i ++; } Quote Link to comment https://forums.phpfreaks.com/topic/279420-to-wrap-alternate-paragraphs-in-tags/#findComment-1437216 Share on other sites More sharing options...
peppericious Posted June 21, 2013 Author Share Posted June 21, 2013 Excellent, thank you!.. Quote Link to comment https://forums.phpfreaks.com/topic/279420-to-wrap-alternate-paragraphs-in-tags/#findComment-1437253 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.