Jump to content

to wrap alternate paragraphs in tags


Go to solution Solved by gristoi,

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

  • Solution

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


 
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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