I pass an array of html code to a php function with the intention of extracting and returning a subset of it.
Here's a partial example of the data passed in:
[0] => <div class="positionIs" style="float:right;width:181px;">
[1] => <table class="datab" style="width:100%;">
[2] => <tr>
[3] => <th colspan="2" style="padding:4px;">Position ist...
[4] => </th>
[5] => </tr>
[6] => <tr class="hell">
[7] => <td style="text-align:left;">...tatsächliche Position
[8] => </td>
[9] => <td style="font-weight:bold;padding:2px;">
[10] => <div style="text-align: center;border:1px solid black;padding:1px;height:1.4em;width:1.4em;background-color:#ffffff;">
[11] => </div>
To return, e.g. the first table row ([2] - [5]) it seems to me that I need to test for "
I've tried to do it with strpos, but it just doesn't work as I expect it to. That's probably because of the presence of the "<" and "/" in the strings. I've tried escaping it with a backslash but that didn't work either. How should I test these strings?
As requested below, here's the code from my function:
$str_out = '';
$tracker = 0;
$start = '<tr';
$end = '/tr';
for ($i = 0, $ii = count($arr_in); $i < $ii; $i++)
{
$str_out .= $arr_in[$i];
if (strpos($arr_in[$i], $start) === true)
{
$tracker++;
}
if (strpos($arr_in[$i], $end) === true)
{
$tracker--;
}
if (!$tracker) break;
}
strpos
won't return TRUE
. It will return the position the first character of the 'needle' is found or return FALSE
if it has not been found. For more info check the manual here: http://php.net/manual/en/function.strpos.php
So instead of using
if (strpos($arr_in[$i], $start) === true)
you can check for non-false like this:
if (strpos($arr_in[$i], $start) !== false)