preg_match_all no results - php

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', ...

Related

How to get a content from table rows as key/value pairs in regex only

I have this table:
<?php
$a ="<table class='table table-condensed'>
<tr>
<td>Monthely rent</td>
<td><strong>Fr. 1'950. </strong></td>
</tr>
<tr>
<td>Rooms(s)</td>
<td><strong>3</strong></td>
</tr>
<tr>
<td>Surface</td>
<td><strong>93m2</strong></td>
</tr>
<tr>
<td>Date of Contract</td>
<td><strong>01.04.17</strong></td>
</tr>
</table>
What I need is to get the value of each <td> inside <tr> as key value pairs as in:
monthly rent => Fr. 1'950.
Rooms(s) => 3
Surface => 93m2
Date of Contract => 01.04.17;
So, far only this code returns some result close to what I need but not like the format I was expecting
preg_match_all("/<td>.*/", $a, $matches);
I am trying to find any improvements on this.
You can use the following regex to get the contents from table rows as key/value pairs :
regex to get keys >> (?<=<td>)(?!<strong>).*?(?=<\/td>)
. . . values >> (?<=<strong>).*?(?=<\/strong>)
see demo
PHP
<?php
$re = '/(?<=<strong>).*?(?=<\/strong>)/';
$str = '<table class=\'table table-condensed\'>
<tr>
<td>Monthly rent</td>
<td><strong>Fr. 1\'950. </strong></td>
</tr>
<tr>
<td>Rooms(s)</td>
<td><strong>3</strong></td>
</tr>
<tr>
<td>Surface</td>
<td><strong>93m2</strong></td>
</tr>
<tr>
<td>Date of Contract</td>
<td><strong>01.04.17</strong></td>
</tr>
</table>';
preg_match_all($re, $str, $matches);
print_r($matches);
?>

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.

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')

What's wrong with this preg_match_all

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

Categories