Jump to content

to wrap alternate paragraphs in tags


peppericious

Recommended Posts

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.
Link to comment
https://forums.phpfreaks.com/topic/279420-to-wrap-alternate-paragraphs-in-tags/
Share on other sites

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 ++;
}


 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.