What's wrong with this preg_match_all - php

I'm using file_get_contents to read a .html file that has a table.
<table id="someTable" style="width:100%;margin-bottom:0;">
<tr style="display:none;">
<td style="padding-left:25px;">Some text</td>
</tr>
<tr style="display:none;">
<td style="padding-left:25px;">another text</td>
</tr>
</table>
When I use preg_match_all to read the table, I get nothing when I count $matches[1]
preg_match_all('/<table id="someTable" style="width:100%;margin-bottom:0;">(.*)<\/table>/', $html, $matches);
$co = count($matches[1]);

Add modifier s to your preg_match.
preg_match_all('/<table id="someTable" style="width:100%;margin-bottom:0;">(.*)<\/table>/s', $html, $matches);
See http://ideone.com/3w0K2

Related

PHP regular expression to extract html values

I want to extract values from the code below.
<tbody>
<tr>
<td><div class="file_pdf">note1</div></td>
<td class="textright">110 KB</td>
<td class="textright">106</td>
</tr>
<tr>
<td><div class="file_pdf">note2.pdf</div></td>
<td class="textright">44 KB</td>
<td class="textright">104</td>
</tr>
</tbody>
I want to extract 'note1', 'note2' strings and 1628 and 1629 numbers.
i treid
preg_match_all('~(\'\)\">(.*?)<\/a>)~', $getinside, $matches);
but its result is not what I am looking for..
is there any simple RegEx to extract them? Thanks!
It should work for you:
preg_match_all("~downloadFile\('(\d+)'\)\">([^<]*)</a>~", $getinside, $matches);
Remember: If your html is very large/complex and you also need to parse more other things from there, then regex is not a better option to do this.

Get data from table using regex php

I want to extract some data from a table using php preg_match_all(). I have the html as under,
I want to get the values in td,
say Product code: RC063154016.
How can I do that? I don'y have any experience with regex,
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><span>Product code:</span> RC063154016</td>
<td><span>Gender:</span> Female</td>
</tr>
</tbody>
</table>
Use DomDocument
$str = <<<STR
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><span>Product code:</span> RC063154016</td>
<td><span>Gender:</span> Female</td>
</tr>
</tbody>
</table>
STR;
$dom = new DOMDocument();
#$dom->loadHTML($str);
$tds = $dom->getElementsByTagName('td');
foreach($tds as $td){
echo $td->nodeValue . '<br>';
}
OUTPUT
Product code: RC063154016
Gender: Female
This should do for you:
preg_match_all('|<td><span>Product code:</span>([^<]*)</td>|', $html, $match);
But if you think there can be random white spaces around tags, then this one:
preg_match_all('|<td>\s*<span>\s*Product code:\s*</span>([^<]*)</td>|', $html, $match);
$data = <<<HTML
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><span>Product code:</span> RC063154016</td>
<td><span>Gender:</span> Female</td>
</tr>
</tbody>
</table>
HTML;
if(preg_match_all('#<td>\s*<span>Product code:</span>\s*([^<]*)</td>#i', $data, $matches)) {
print_r($matches);
}
Use any one parser and parse the HTML and use it. Don't use preg* functions here. Please read this answer How do you parse and process HTML/XML in PHP?

PHP Table Reader

How to read and get the ISP value from html table?
<table style="padding-top:10px;">
<tbody>
<tr>
<th>ISP:</th>
<td>My Provider</td>
</tr>
<tr><th>Organization:</th><td nowrap=""></td>
</tr>
<tr><th>Connection:</th>
</tbody></table>
Given you lack of information, a regular expression would be the easiest solution.
$matches = array();
preg_match("<th>ISP:</th>[\r\n\s\t]*<td>(.*)</td>", "<th>ISP:</th><td>My Provider</td>...", $matches);
var_dump($matches);

preg_match_all() Uses

I have a variable $html
in which this code is stored
<form action="track_mobile.asp" method="post" name="TrackMobile">
<table width="99%" height="55" border="0" cellpadding="2" cellspacing="0">
<tr>
<td class="Heading2" colspan="2"> Track Any Mobile Location</td>
</tr>
<tr>
<td width="21" valign="top" rowspan="2" class="s2stextbox" valign="top"><img src="../images/operators_logo/Tata.png" width="134" height="121" align="left"> </td>
<td width="897" valign="top" class="s2stextbox"><font size="2"><b>Mobile Number: </b>918888888888</font></td>
</tr>
<tr>
<td width="897" valign="top" class="s2stextbox" valign="top"><font size="2"> <b>User Name:</b> We are unable to trace the Name for this Mobile Number<br>
<font size="2"><b>Mobile Operator Name:</b> TATA TELESERVICES<br>
<b>State/Region: </b>Maharashtra</font></td>
</tr> </table>
</form>
In which every item after ":(semicollon)" are random which comes different every time.
Plz give me correct syntax to get echo as
Mobile Number: 918888888888
User Name: We are unable to trace the Name for this Mobile Number
Mobile Operator Name: IDEA
State/Region: Maharashtra
in which
these are ramdomly generated, different evey time so, that preg_match search this loaction and echo the text which are there in same loaction
918888888888
We are unable to trace the Name for this Mobile Number
IDEA
Maharashtra
1. Strip-out HTML tag
$text = strip_tags($html);
2. Match using text before column
I just change the regexp and display each value match by (.*) (everything that follow the pattern up to the end of line:
preg_match('/Mobile Number: (.*)/', $html, $matches);
echo $matches[1];
preg_match('/User Name: (.*)/', $html, $matches);
echo $matches[1];
preg_match('/Mobile Operator Name: (.*)/', $html, $matches);
echo $matches[1];
preg_match('/State/Region: (.*)/', $html, $matches);
echo $matches[1];
This is a work for XPath (see [SimpleXMLElement::xpath][1], your XPaths look like be:
918888888888
/form/table/tr[1]/td[2]/font[substring-after(./text(), ':') ->
We are unable to trace the Name for this Mobile Number
/form/table/tr[2]/td[1]/font[1][substring-after(./text(), ':')
TATA TELESERVICES
/form/table/tr[2]/td[1]/font[2][substring-before(substring-after(./text(), ':'), 'State')
Maharashtra
/form/table/tr[2]/td[1]/font[2][substring-after(./text(), 'Region')

preg_match_all no results

preg_match_all('|<tr>(.*?)</tr>|', '<table>
<tr>
<td>oo</td>
</tr>
<tr>
<td>ddd</td>
</tr>
</table>', $matches, PREG_PATTERN_ORDER);
why this doesn't show any results.
I want to get second match $matches[1][2]
You need to use the s pattern modifier
preg_match_all('|<tr>(.*?)</tr>|s', ...

Categories