Jump to content

extract text from html-fil


torvald_helmer

Recommended Posts

I have a html-file, which I want to extract the some content, and not any tag's.

 

I thoguht I might start like this:

$file = file("test.html");

foreach ($file as $line) {

 

some code....

 

}

 

A example is this line is the file:

<tr><td class="felt">Car</td><td>Mercedes<br/></td></tr>

 

How can I get just 'Car' and 'Mercedes' from this line?

 

Has anyone got an idea? I really don't know where to go further...

Need help!

Link to comment
https://forums.phpfreaks.com/topic/49364-extract-text-from-html-fil/
Share on other sites

In this case, I would mess a little with explode

explode(splitter, haystack);

 

When you explode, you chop a string in an array. so when you explode on > you can get the usefull things out with the keys...

For example:

 

<?php
$string = 'Here are more HTML tags ... <td>Example</td> ... Here are more HTML tags';
$string = explode('<td>', $string); // key 1 contains 'Example</td>' (key 0 is empty)
$string = $string[1];
$string = explode('</td>', $string); // key 0 contains 'Example'
$string = $string[0];
?>

 

Though you need to be carefull with the keys

You can write simple functions for this to make it an easier job, hope this function is usefull

 

Grtz

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.