0
<?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;

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).
Rahul K
  • 413
  • 3
  • 11
  • 3
    don't use regexes. use [DOM](http://php.net/dom) – Marc B Jul 21 '16 at 15:14
  • i really don't know how can i use dom and bold title with this content in a variable. – Rahul K Jul 21 '16 at 15:15
  • You want *only* the title in bold, or also *each* "Choice X"? What about the very first line in CAPS, should it also be in bold? – trincot Jul 21 '16 at 15:24
  • If you Google "best answer on Stack Overflow" ... oddly enough, this comes in at #4 (at the time of writing) : http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – CD001 Jul 21 '16 at 15:28
  • `Choice E` is bolded for me. What is the expected result? https://eval.in/609522 Why are the `@`s present? Also the `m` modifier doesn't do anything special without the `^` or `$` being used. – chris85 Jul 21 '16 at 15:51
  • "THIS IS MY MAIN TITLE IN CAPS" this word only i needs this in bold but remember title always not same. Also, i am checking how it is possible to bold only title. – Rahul K Jul 22 '16 at 07:07
  • where does that markup come from? it might be easier to change it from the source instead of from the output – Gordon Aug 02 '16 at 06:20
  • In PHP you can see $data have some string and the 3rd point have issues you can just run from your local server so, you can get that issue easily when lessthen/graterthen signs comes it will skip line. – Rahul K Aug 02 '16 at 06:25

2 Answers2

1

Regular expression could indeed be used to deal with this, but in general it is advisable to perform HTML manipulation through a DOM. PHP's DOMDocument provides this.

You could then use this code, which walks through all text nodes and sees if any of the two conditions are met:

  • The text starts with words in a predefined list
  • The text is entirely in upper case

In both cases a new strong node is created with that content, and the original node is adapted accordingly.

$dom = new DOMDocument();
$dom->loadHTML($data, LIBXML_HTML_NOIMPLIED | 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();

See it run on ideone.com

trincot
  • 317,000
  • 35
  • 244
  • 286
  • Its working good, but i not needs choices in bold format just the title. – Rahul K Jul 22 '16 at 12:37
  • You're welcome. I assume you were able to take out the part you did not need. – trincot Jul 22 '16 at 13:13
  • 1
    ok thanks, i have get this code clearly and next time use dom its better. – Rahul K Jul 22 '16 at 13:23
  • I have added some little changes i want to check this above words as needed from this content and make it bold. – Rahul K Jul 22 '16 at 14:20
  • Updated my answer accordingly. Just use regular expression to match the text in each node. The code was already there for "Choice X". Just had to change it to "TEST ABC:|TEST XYZ: " ...etc. – trincot Jul 22 '16 at 15:17
  • Thanks, for helping all of them changes now its working good. – Rahul K Jul 23 '16 at 05:27
  • I have added 3rd point and trying to get less then grater then line to this php dom code. – Rahul K Aug 02 '16 at 05:58
  • I have added 3rd point and trying to allow less than grater than signs in string line to this php dom code. – Rahul K Aug 04 '16 at 12:31
  • If you have a new question, please ask it as a new question instead of adding new points to your existing question. If you refer me to your new question, I will be happy to look at it. – trincot Aug 04 '16 at 12:56
  • you can see here my new question have already asked: [http://stackoverflow.com/questions/38719668/php-dom-lessthan-graterthan-sign-should-not-skip-a-line] – Rahul K Aug 04 '16 at 13:27
  • you can see here my new question here if you can solve this problem: http://stackoverflow.com/questions/38800875/using-php-dom-want-to-show-all-string-as-a-output – Rahul K Aug 06 '16 at 05:52
0
echo preg_replace('/(THIS IS MY MAIN TITLE IN CAPS)/', '<strong>$1</strong>', $data);

Preg_replace syntax : preg_replace(regex, replace, subject)

$1 will display the title captured under parentheses in the regex

  • While this code may answer the question, providing additional context regarding *how* and/or *why* it solves the problem would improve the answer's long-term value. – Michael Parker Jul 21 '16 at 16:56
  • you are right but title not always same so, i can't check title directly. After last choice title comes and i need it just bold. – Rahul K Jul 22 '16 at 07:01