Preg_match Easiest Way To Match Text From Inside Html Tags
Solution 1:
I normally suggest if you need to actually express what you're looking for in a HTML document to use an xpath
expression for that because it can give you the actual value whereas regex'es are not able to further parse the HTML/XML, and xpath
expressions are much more fine-grained. See the output which returns the text-value for example w/o any further tags inside:
array(8) {
[0]=>
string(16) "Text text text:3"
[1]=>
string(14) "Text text text"
[2]=>
string(1) "1"
[3]=>
string(1) "0"
[4]=>
string(1) "2"
[5]=>
string(4) "2.90"
[6]=>
string(4) "3.20"
[7]=>
string(4) "1.85"
}
Code:
$html = <<<EOD
<tablewidth="100%"border="0"cellspacing="0"cellpadding="0"class="rowData"><tralign="center"class="fnt-vrdana-mavi" ><tdstyle="font-size:11px"colspan=3><b>Text text text</b>:3</td></tr><trclass="header"align="center"><tdheight="18"colspan="3">Text text text</td></tr><tralign="center"class="fnt-vrdana"bgcolor="#eff3f4"height="18"><tdwidth="32%"height="17"><b>1</b></td><tdwidth="34%"><b>0</b></td><tdwidth="34%"><b>2</b></td></tr><tralign="center"class="fnt-vrdana-mavi"><tdheight="17">2.90</td><td>3.20</td><td>1.85</td></tr></table>
EOD;
// create DomDocument to operate xpath on
$doc = new DomDocument;
$doc->loadHTML($html);
// create DomXPath
$xpath = new DomXPath($doc);
// perform the XPath query
$nodes = $xpath->query('//td');
// process nodes to return their actual value
$values = array();
foreach($nodes as $node) {
$values[] = $node->nodeValue;
}
var_dump($values);
Solution 2:
/<td.*?>(.*?)<\/td>/
would get all data between the <td>
and </td>
.
Getting the data from inside a <td>
tag would be /<td([^>]*)>/
or /<td(.*?)>/
Element?
I have a table and I need the cells to have an element posi…
Here's a js fiddle of what I currently have, I am tryin…
So I have a few pictures that I want to randomly generate o…
This is my first question. I usually try to research these …
Right Floated Element Disappears When Using Columns In Firefox
I am using an ol element with column-count and column-gap p…
Setting Link Href From Child Image Src Using JQuery
I am using .wrap() to create a link around each image withi…
How To Limit The Html Table Records In Each Page
I am populating a table in my jsp which has more than 20 re…
I want to show countdown in each of my divs. Right now I ge…
Retain Dynamically Created DropDown List's Selected Value On Event Window.history.back()- JavaScript
Can use the below question as a reference to explain the co…
|
Post a Comment for "Preg_match Easiest Way To Match Text From Inside Html Tags"