0

I have tried 1st, 2nd points are working good just have to solve 3rd issue:
1. Title with bold: "THIS IS MY MAIN TITLE IN CAPS" (title not always same)
2. Words with bold: TEST ABC:, TEST XYZ:, TES T TEST:, TESTXXX: (this words are always same)
3. Some strings are not showing skipping a line when you run this code (lessthen and graterthen in string forex: <140/90 mmHg OR <130/80 mmHg).

<?php
$data = 'THE CORRECT ANSWER IS C.
<p>Choice A Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s</p>
<p></p>
<p>Choice B Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s</p>
<p>Choice D Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s</p>
<p></p>
<p>Choice E simply dummy text of the printing and typesetting industry.</p>
<p></p>
<p><br>THIS IS MY MAIN TITLE IN CAPS<br>This my sub title.</p>
<p><br>TEST ABC: Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
<p>1) It is a long established fact <140/90 mmHg OR <130/80 mmHg making it look like readable English will uncover many web sites still in their infancy. 
<br><br>2) There are many variations of passages of Lorem Ipsum available. </p>
<p><br>TEST XYZ: Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
<p><br>TES T TEST: It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
<p><br>TESTXXX: It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>';

$dom = new DOMDocument();
@$dom->loadHTML($data, LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//text()') as $node) {
    $txt = trim($node->nodeValue);
    $p   = $node->parentNode;
    if (preg_match("/^\s*(TEST ABC:|TEST XYZ:|TES T TEST:|TESTXXX)(.*)$/s", $node->nodeValue, $matches)) {
        // Put Choice X in bold:
        $p->insertBefore($dom->createElement('strong', $matches[1]), $node);
        $node->nodeValue = " " . trim($matches[2]);
    } else if (strtoupper($txt) === $txt && $txt !== '') {
        // Put header in bold
        $p->insertBefore($dom->createElement('strong', $txt), $node);
        $node->nodeValue = "";
    }
}
$data = $dom->saveHTML();
echo $data;
2
Contributors
1
Reply
18
Views
1 Year
Discussion Span
Last Post by cereal
1

Hi,

it happens because in <140/90 mmHg OR <130/80 mmHg) there is the < character that has a special meaning in the HTML parsing. Example:

<?php

$html = '<p>Hello < 140 / 32 World</p>';
$dom  = new DomDocument();
$dom->loadHTML($html);

print $dom->saveHTML();

It returns <p>Hello </p> (I've stripped the rest of the HTML).

To solve you could strip all the tags, or replace them with placeholders, see Textile for example, convert to HTML entities all the special characters and then inject them back in the page.

But it's not worth if this has to be done frequently. In my opinion you should:

  • save questions and answers into a database;
  • apply an encoding (UTF-8 for example) to these strings: in the database, in the server so that requests and responses are treated the same, and in the HTML page, so that the browser will display them correctly;
  • use a template to generate the pages: so that you have to change only one file;
  • use approriate tags: titles and lists instead of paragraphs everywhere;
  • use external styles to change the appearance of the elements

or, if semantically relevant, you could just modify the template to add tags like strong, but operate on a template instead of altering multiple static files.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.