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.

3

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

0
$buffer = preg_replace('%<table>(.*?)</table>%sim', '<table><div class="table">$1</div></table>', $buffer);

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.