0

I have code on a side which looks like the one below and will be generated from a CMS. The user can generate a table, but I have to put a <div> around it.

<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et 
dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo 
dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem</p>

<table>
<thead>
    <tr><td></td></tr>
    ...
</tbody>
</table>

<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et 
dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo 
dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem</p> 
<table>
<thead>
    <tr><td></td></tr>
    ...
</tbody>
</table>
...

My goal is it now to give every <table> a <div class="table">

I´ve tried it with regex and got this result:

function smarty_modifier_table($string) {
    preg_match_all('/<table.*?>(.*?)<\/table>/si', $string, $matches);
    echo "<pre>";
    var_dump($matches);
}
/* result
array(2) {
    [0]=> string(949) "<table>...</table>"
    [1]=> string(934) "<thead>...</tbody>"
}
array(2) {
    [0]=> string(949) "<table>...</table>"
    [1]=> string(934) "<thead>...</tbody>"
}
*/

First of all, I do not understand why the second array [1]=> string(934) "<thead>...</tbody>" appears and second how to fit the modified array back into the string on the right place.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ToTe
  • 51
  • 1
  • 8
  • 1
    regexp is not the right tool for that. See http://stackoverflow.com/questions/6751105/why-its-not-possible-to-use-regex-to-parse-html-xml-a-formal-explanation-in-la – cadrian Apr 15 '14 at 08:07
  • 1
    `array[1]` is the group matches by the first pair of parenthesis. – Toto Apr 15 '14 at 08:19
  • Possible duplicate of [Regex matching table rows in HTML](https://stackoverflow.com/questions/7289181/regex-matching-table-rows-in-html) – Brian Tompsett - 汤莱恩 Oct 29 '17 at 09:03

3 Answers3

0
$buffer = preg_replace('%<table>(.*?)</table>%sim', '<table><div class="table">$1</div></table>', $buffer);
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

If your html is really simple like this, the following would probably work:

print preg_replace('~<table.+?</table>~si', "<div class='table'>$0</div>", $html);

If, however, you can have nested tables:

<table>
    <tr><td> <table>INNER!</table> </td></tr>
</table>

this expression will fail miserably - that's why using regexes to parse html is not recommended. To handle complex html it's better to use a parser library, for example, XML DOM:

$doc = new DOMDocument();
$doc->loadHTML($html);
$body = $doc->getElementsByTagName('body')->item(0);
foreach($body->childNodes as $s) {
    if($s->nodeType == XML_ELEMENT_NODE && $s->tagName == 'table') {
        $div = $doc->createElement("div");
        $div->setAttribute("class", "table");
        $body->replaceChild($div, $s);
        $div->appendChild($s);
    }
}

This one handles nested tables correctly.

gog
  • 10,367
  • 2
  • 24
  • 38
0

Thank you all for your incredible fast and perfect help! So it works for me.

$result = preg_replace('~~si', "$0", $string);

return $result;

regards

Torsten

ToTe
  • 51
  • 1
  • 8