Search code examples
phpregextwitter-bootstrappreg-match

php preg_match table and wrapping div


I have CMS driven content and when saving prep the content, as part of that, I want to clean the tables the authors create.

We use BootStrap on the front end, so want to be able to first - grab all tables. Check the parent elements, if it is not <div class="table-resposnsive">, wrap it in that.

I have:

// $content = $_POST['content']; 
// Set some TEST content
$content = "<h1>My Content</h1>
    <p>This is some content</p>
    <table border=\"1\">
        <tr>
            <td>cell</td>
            <td>cell</td>
            <td>cell</td>
        </tr>
        <tr>
            <td>cell</td>
            <td>cell</td>
            <td>cell</td>
        </tr>
    </table>
    <div align=\"center\">see the above content</div>
    <p>Thanks!</p>\n\n";

// Make our example content longer with more variations... 
$content = $content . 
    str_replace('<table border="1">', '<table border="0" class="my-table">', $content) . 
    str_replace('<table border="1">', '<table border="0" cellpadding="0" cellspacing="3">', $content);

$output  = $content;

// Parse for table tags
preg_match_all("/<table(.*?)>/", $content, $tables);

// If we have table tags.. 
if(count($tables[1]) > 0) {

    // loop over and get teh infor we want to build the new table tag. 
    foreach($tables[0] as $key => $match) {
        $add_class = array();
        $tag = ' '. $tables[1][$key] .' ';
        $add_class[] = 'table';
        // check if we have got Borders....
        // If we do. add the bootstrap table-border calss. 
        if(strpos($tag, 'border="0"') === FALSE) {
            $add_class[] = 'table-bordered';
        }
        // prepend any existing/custom classes. 
        if(strpos($tag, 'class="') > 0) {
            preg_match("/class=\"(.*?)\"/", $tag, $classes);
            if($classes[1]) {
                $add_class = array_merge($add_class, explode(' ', $classes[1]));
            }
        }
        // add classes. 
        $add_class = array_unique($add_class);

        // Now - replace the original <table> tag with the new BS tag. 
        // adding any class attrs
        // wrap in the responsive DIV. - THIS part - needs to be only added if its not already wrapped... 
        // this would happen if we have already edited the page before right ... 
        $output = str_replace($match, '<div class="table-responsive">'."\n".'<table class="'. implode(' ', $add_class) .'">', $output);

    }

    // replace all closing </table> tags with the closing responsive tag too... 
    $output = str_replace('</table>', '</table>'."\n".'</div>', $output);

} 

echo highlight_string($content, TRUE);
echo '<hr>';
echo highlight_string($output, TRUE);

Solution

  • You can use Simple HTML dom parser do select divs https://github.com/sunra/php-simple-html-dom-parser

    $html = new simple_html_dom();
    $html->file_get_html(__filepath__);
    # get an element representing the second paragraph
    $element = $html->find("#youdiv");`
    

    Good luck