0

I am trying to write a script that replaces some html tags into other or more html tags. Choose preg_replace because of the nice effect with variables in it. For Example:

$xhtmlcode = preg_replace('@<h(.*?)>(.*?)</h(.*?)>@is','<h$1><sub>$2</sub></h$3>', $xhtmlinput);

This above code works and gives me an expecting result, but then after it i do a:

$xhtmlcode = preg_replace('@<li(.*?)>(.*?)</li>@is','<li><sub>$2</sub></li>', $xhtminput);

just pays NULL as output..

The same thing happens when work with other code like this, now I realy like to know why this happens:

  $xhthmlintput = '
  <span style="font-style:italic;font-weight:bold;font-size:12.5px">this is the test world</span>
  <span style="font-size:5.5px">this is the test planet</span>
  <span style="font-size:5.5px">test it well</span>';

AND i got the same problem again with:

$xhtmlcode = preg_replace( '@<span style="(.*?)bold(.*?)>(.*?)</span>@i', '<b>$3</b>', $xhtmlintput );

I get troubles again with a NULL as output again.

scher200
  • 1
  • 1
  • show us some example content, what you want to replace, with what... – vaso123 Nov 13 '14 at 13:32
  • You obtain NULL because there is no 2nd capture group. You need to use `$1` instead of `$2`. But you will better do this with DOMDocument. – Casimir et Hippolyte Nov 13 '14 at 14:57
  • You where right Casimir, I posted the wrong code there, just edited the right one. – scher200 Nov 14 '14 at 07:57
  • 1
    [Don't use regex to modifiy HTML](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454)! Use `DOMDocument` and `DOMXpath` instead – SBH Nov 14 '14 at 08:16
  • Why don't you just use CSS to change what H1, H2, LI, etc. look like? – Ja͢ck Nov 14 '14 at 08:48
  • thank you for the DOMDocument idea it works great for a lot of stuff, but still why is this code up here nog doing its regex thing? – scher200 Nov 20 '14 at 00:50
  • @scher200 you get null due to typo - you wrote `$xhtmlintput` instead of `$xthmlintput` – Cheery Nov 20 '14 at 01:28
  • thanks you Cheery, but sadly it is still giving me NULL – scher200 Nov 20 '14 at 15:35

0 Answers0