Skip to content
Advertisement

PHP – Extract a cell value of a table with a match expression

I want to extract the value of a specific cell from a table in a web page. First I search a string (here a player’s name) and after I wan’t to get the value of the <td> cell associated (here 94).

I can connect to the web page, find the table with is id and get all values. I also can search a specific string with preg_match but I can’t extract the value of the <td> cell.

What the best way to extract the value of a table with a match expression ?

Here is my script :

JavaScript

Here is the HTML structure of the table :

JavaScript

Advertisement

Answer

DOM manipulation solution.

Search over all cells and break if cell consists LeBron James value.

JavaScript

Regex expression for whole cell value with name LeBron James.

JavaScript

If you want to capture also ID 94 from next cell you can use this expression.

JavaScript

It returns two groups, first cell with player’s name and second with ID.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement