0

help me with the code

$homepage = file_get_contents('index.html');
foreach($homepage->find('table') as $table){ 
     // returns all the <tr> tag inside $table
     $all_trs = $table->find('tr');
     $count = count($all_trs);
     echo $count;
}

i am trying to get content of index.html and then count the number of tr tags but the above code isn't working.

2 Answers 2

1

You can use the DOM API for this purpose:

$dom = new DOMDocument;
$dom->loadHTMLFile('index.html');
$tr_count = $dom->getElementsByTagName('tr')->length;
Sign up to request clarification or add additional context in comments.

Comments

0

I'm not entirely sure what context this code is in, but you can use file_get_contents() and substr_count() to do what you want.

http://php.net/manual/en/function.file-get-contents.php

http://php.net/manual/en/function.substr-count.php

echo substr_count(strtolower(file_get_contents('index.html')), '<tr>');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.