0

Updated: (things are more complicated including blocks, which I didn't explain from the start, but I understand that this should work with regex or something)

How to parse HTML blocks to a table layout for every not empty tag? As an example, this HTML:

<p class="block1">
    <span class="styleclass2">
        <span class="styleclass25">
            <strong>
                <u></u>Some Text Here
            </strong>
            <br>
        </span>
    </span>
    <span class="styleclass5">
        <u>
            <a href="http://www.example.com">www.example.com</a>
        </u>
    </span>
    <br>
    <span class="styleclass24">Some Text Here</span>
</p>
<p class="block2">
    <span class="styleclass2">
        <span class="styleclass25">
            <strong>
                <u></u>Some Text Here2
            </strong>
            <br>
        </span>
    </span>
    <span class="styleclass5">
        <u>
            <a href="http://www.example2.com">www.example2.com</a>
        </u>
    </span>
    <br>
    <span class="styleclass24">Some Text Here2</span>
</p>

and make these:

<table>
    <tr>
        <td>Some Text Here</td>
        <td>www.example.com</td>
        <td>Some Text Here</td>
    </tr>
    <tr>
        <td>Some Text Here2</td>
        <td>www.example2.com</td>
        <td>Some Text Here2</td>
    </tr>
</table>

The main idea is how to group this blocks to make a row for every block found...

user1844923
  • 179
  • 1
  • 11
  • 1
    Please post the PHP code that you are having trouble with. We aren't going to write it for you. – mcryan Feb 21 '13 at 15:32

2 Answers2

1

Regular Expressions are magic, try this:

<?php
$string = '<p class="styleclass1">
    <span class="styleclass2">
        <span class="styleclass25">
            <strong>
                <u>Some Text Here</u>
            </strong>
            <br>
        </span>
    </span>
    <span class="styleclass5">
        <u>
            <a href="http://www.example.com">www.example.com</a>
        </u>
    </span>
    <br>
    <span class="styleclass24">Some Text Here</span>
</p>';

$result = preg_match_all("/<\w+.*?>(.*?)<\/\w+>/", $string, $matches);

echo '<pre>';
print_r($matches);
echo '</pre>';

$output = '<table style="border: 1px solid #000;">';

foreach ($matches[1] as $key => $value) {
    $output .= '<tr>';
    $output .= '<td>'.$value.'</td>';
    $output .= '</tr>';
}

$output .= '</table>';

echo $output;
?>
  • It works well, but have an issue when the text is out from the tag and stands in outer tag, like this: "Some Text Here" instead of: "Some Text Here" – user1844923 Feb 22 '13 at 07:41
0

hodgie code :)

$html = <<<HERE
<p class="styleclass1">
    <span class="styleclass2">
        <span class="styleclass25">
            <strong>
                <u>Some Text Here</u>
            </strong>
            <br>
        </span>
    </span>
    <span class="styleclass5">
        <u>
            <a href="http://www.example.com">www.example.com</a>
        </u>
    </span>
    <br>
    <span class="styleclass24">Some Text Here</span>
</p>
HERE;
preg_match_all('#>(.+)<#sU', $html, $matches);
if(isset($matches[1]))
{
    echo '<table border="1">';
    foreach($matches[1] as $val)
    {
        $val = trim($val);
        if(!empty($val))
            echo '<tr><td>' . $val . '</td></tr>';
    }
    echo '</table>';
}
Winston
  • 1,758
  • 2
  • 17
  • 29