regex - Using PHP DOM to change title in bold format and other string modifications -


<?php $data = 'the correct answer c. <p>choice lorem ipsum dummy text of printing , typesetting industry. lorem ipsum has been industrys standard dummy text ever since 1500s</p> <p></p> <p>choice b lorem ipsum dummy text of printing , typesetting industry. lorem ipsum has been industrys standard dummy text ever since 1500s</p> <p>choice d lorem ipsum dummy text of printing , typesetting industry. lorem ipsum has been industrys standard dummy text ever since 1500s</p> <p></p> <p>choice e dummy text of printing , typesetting industry.</p> <p></p> <p><br>this main title in caps<br>this sub title.</p> <p><br>test abc: lorem ipsum dummy text of printing , typesetting industry.</p> <p>1) long established fact <140/90 mmhg or <130/80 mmhg making readable english uncover many web sites still in infancy.  <br><br>2) there many variations of passages of lorem ipsum available. </p> <p><br>test xyz: lorem ipsum has been industrys standard dummy text ever since 1500s, when unknown printer took galley of type , scrambled make type specimen book.</p> <p><br>tes t test: has survived not 5 centuries, leap electronic typesetting, remaining unchanged.</p> <p><br>testxxx: popularised in 1960s release of letraset sheets containing lorem ipsum passages, , more desktop publishing software 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()') $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 working have solve 3rd issue:

  1. title bold: "this main title in caps" (title not same)
  2. words bold: test abc:, test xyz:, tes t test:, testxxx: (this words same)
  3. some strings not showing skipping line when run code (lessthen , graterthen in string forex: <140/90 mmhg or <130/80 mmhg).

regular expression indeed used deal this, in general advisable perform html manipulation through dom. php's domdocument provides this.

you use code, walks through text nodes , sees if of 2 conditions met:

  • the text starts words in predefined list
  • the text entirely in upper case

in both cases new strong node created content, , original node adapted accordingly.

$dom = new domdocument(); $dom->loadhtml($data, libxml_html_noimplied | libxml_html_nodefdtd); $xpath = new domxpath($dom); foreach($xpath->query('//text()') $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 run on ideone.com


Comments