I have a table in html which I need to convert to a database table for MySQL (I use HeidiSQL). SO there are a few td tags and then a tr tag indicating a new record. How would I be able to insert them in the db table? DO I use preg_match? How would it be written. preg_match_all('|id="(.*)">|U', $contents_of_page, $idout, PREG_SET_ORDER); preg_match_all("|<[^>]+>(.*)</[^>]+>|U", $contents_of_page, $out, PREG_SET_ORDER); PHP:
Here it is: <tbody><tr> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="left">gn</td> <td dir="ltr" align="left">Genesis</td> <td></td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="left">In the beginning God created the heaven and the earth.</td> </tr> <tr> <td dir="ltr" align="right">2</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="left">gn</td> <td dir="ltr" align="left">Genesis</td> <td></td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">2</td> <td dir="ltr" align="right">2</td> <td dir="ltr" align="left">And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters.</td> </tr> </tbody> Code (markup):
Here is an example, I'm still learning regex which is why I gave it a shot <?php $contents =<<<PAGE <tbody><tr> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="left">gn</td> <td dir="ltr" align="left">Genesis</td> <td></td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="left">In the beginning God created the heaven and the earth.</td> </tr> <tr> <td dir="ltr" align="right">2</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="left">gn</td> <td dir="ltr" align="left">Genesis</td> <td></td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">2</td> <td dir="ltr" align="right">2</td> <td dir="ltr" align="left">And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters.</td> </tr> </tbody> PAGE; $va = preg_match_all('/<td(.*) *>(.*)<\/td>/i', $contents, $idout); echo sizeof($idout[0]); echo $idout[0][21]; PHP: Do note that you have to take care of empty tds (just use empty() ) Hope this helps, test with it etc etc.
. = any character * = allows any number of the same thing, but optional () are alternation class where you can set rules and subrules and so on \ = escapes the data /i = tells the engine that we are not looking for case sensitive matches *meaning i stands for insensitive*
The delimiters. Each regular expression has to go between these. (It can be pretty much anything, ~, /, {, !, @, etc...) Take a look at this video: http://www.phpvideotutorials.com/regex/
innerHTML as in reading the insides of <td> tags? I think you have to do a var_dump($contents); It does read the attributes, well use this, I just realized I added a class where I shouldn't <?php $contents =<<<PAGE <tbody><tr> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="left">gn</td> <td dir="ltr" align="left">Genesis</td> <td></td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="left">In the beginning God created the heaven and the earth.</td> </tr> <tr> <td dir="ltr" align="right">2</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="left">gn</td> <td dir="ltr" align="left">Genesis</td> <td></td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">1</td> <td dir="ltr" align="right">2</td> <td dir="ltr" align="right">2</td> <td dir="ltr" align="left">And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters.</td> </tr> </tbody> PAGE; $va = preg_match_all('/<td .*>(.*)<\/td>/i', $contents, $idout); echo sizeof($idout[0]); var_dump($idout); PHP: This should give you td insides without the attributes. Do test with it further.