confusing preg_match_all function
HTML Code:
<table >
<tr>
<td></td>
</tr>
<tr>
<td class=\"Contents\">
<p>Large Paragraph1....</p>
<p>Large Paragraph1....</p>
<p>Large Paragraph1....</p>
<p>Large Paragraph1....</p>
</td>
<tr>
<td class=\"Contents\">
<p>Large Paragraph 2</p>
<p>Large Paragraph 2</p>
<p>Large Paragraph 2</p>
<p>Large Paragraph 2</p>
<p>Large Paragraph 2</p>
</td>
</tr>
</table>
I want to get contents of cell which contains ID as MainCell.
So I have wrote a sample php code like this
PHP Code:
<?php $html="<table >
<tr>
<td></td>
</tr>
<tr>
<td class=\"Contents\">
<p>Large Paragraph1....</p>
<p>Large Paragraph1....</p>
<p>Large Paragraph1....</p>
<p>Large Paragraph1....</p>
</td>
<tr>
<td class=\"Contents\">
<p>Large Paragraph 2</p>
<p>Large Paragraph 2</p>
<p>Large Paragraph 2</p>
<p>Large Paragraph 2</p>
<p>Large Paragraph 2</p>
</td>
</tr>
</table>";
preg_match_all('#<td class=\"Contents\">(.+?)</td>#', $html, $MainHeads);
echo "<table>";
foreach ($MainHeads[1] as $Contents)
{
echo("<tr>");
echo("<td>.$Contents.</td>");
echo("</tr>");
}
echo "</table>";
?>
But this is not working, What's wrong on this code?
Note: When I change $html like this
PHP Code:
$html="<table >
<tr>
<td></td>
</tr>
<tr>
<td class=\"Contents\"><p>Large Paragraph1....</p></td>
<tr>
<td class=\"Contents\"><p>Large Paragraph 2</p></td>
</tr>
</table>";
It is working
Re: confusing preg_match_all function
Your regular expression is not meant to have hashes in "#" and also, you don't need to use preg_match_all. Use the Dom Extension.
Re: confusing preg_match_all function
Quote:
Originally Posted by visualAd
Your regular expression is not meant to have hashes in "#"
I use hashes. Lots of characters can be used as start and end tokens.
The expression doesn't work because you've unnecessarily escaped the double quote characters. These do not need to be escaped in single-quoted string literals.
However, I would use the DOM (as advised) and an XPath query.
Re: confusing preg_match_all function
Quote:
Originally Posted by penagate
I use hashes. Lots of characters can be used as start and end tokens.
I have never seen this usage. I've always assumed it as // :confused:
Re: confusing preg_match_all function
http://au2.php.net/manual/en/ref.pcre.php
Quote:
The expression should be enclosed in the delimiters, a forward slash (/), for example. Any character can be used for delimiter as long as it's not alphanumeric or backslash (\). If the delimiter character has to be used in the expression itself, it needs to be escaped by backslash.
Re: confusing preg_match_all function
But i have got the correct patten for need from PHP manual.
I agree '#' was a cause for my problem. And another cause was that cell contains multi line style. So i want to put /m on end
But I have found correct patten and modify it as advances .
That patten is
[
Quote:
"/<td\s+.*?class=[\"\']".$Contents."[\"\']?[^>]*>(.+?)<\/td>/ism