Announcement

Collapse
No announcement yet.

questions & answers

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    questions & answers

    I have a variable that I want to put questions and answers into. There maybe one Q&A or lots.

    Can anyone think of how I could parse the variable using php so I can format it nicely? e.g.

    from:
    (q)question text(q)(a)answer text(a)

    to:
    <div class="qa">
    <div class="q">question text</div>
    <div class="a">answer text</div>
    </div>

    or would it be easier just to add this all ot the variable at the time of entry so no parsing is required... or, something else entirely...?

    Thanks,
    Paul.

    #2
    I'd recommend using

    (q)question text(a)answer text(q)question text(a)answer text

    as this assumes that an answer follows a question so there's no need for extra (q) or (a) aty the end of the items.

    Try (untested)
    Code:
    // format
    // (q)question(a)answer(q)question(a)answer.....
    
    $qtext = '<actinic:variable name="Questions" encoding="perl" selectable="false" />';
    if ( $qtext != '' ) 
    	{
    	$qtext = preg_replace('/\(q\)/', '<div class="q">', $qtext, 1);// first (q) doesn't end previous item
    	$qtext = str_replace('(q)', '</div><div class="q">', $qtext);	// subsequent (q) end previous (a)
    	$qtext = str_replace('(a)', '</div><div class="a">', $qtext);	// each (a) ends prior (q) 
    	$qtext .= '</div>';						// end final (a)
    	echo '<div class="qa">' . $qtext . '</div>';
    	}
    Norman - www.drillpine.biz
    Edinburgh, U K / Bitez, Turkey

    Comment


      #3
      excellent! I got it working perfectly for what I need! I did have to change the str_ireplace to str_replace to get it to work, don't know what the difference is, but its all working fine now.

      Thanks,
      paul.

      Comment


        #4
        Ahah - str_ireplace is case insensitive but needs PHP5. The Actinic inbuilt PHP is 4.12. Using str_replace is fine but you'll have to remember to type (q) and (a) in lower-case.

        I've amended my post above in case others use it.
        Norman - www.drillpine.biz
        Edinburgh, U K / Bitez, Turkey

        Comment

        Working...
        X