Separate DIV tags from HTML automatically

Hi,

I am trying to separate the DIV tags from HTML in a text file using the following code. But I’m not able to. Can you please guide me in this one?

$file = ‘Html/’.$name[0].‘.txt’;

$current = file_get_contents($file);

$current .= $contents;

file_put_contents($file, $current);

$file=fopen(‘Html/’.$name[0].“.txt”,“r”) or exit(“Unable to open file!”);
$f=0;
while (!feof($file))
{
$demo[$f]=fgetc($file);
$f = ($f+1);
}
fclose($file);

echo $f;
echo count($demo);
for($i=0;$i<200;$i++){
echo $demo[i];
}
for($i=0;$i< count($demo);$i++)
{

if( $demo[i] == ‘<’ && $demo[i+1] == ‘d’ && $demo[i+2] == ‘i’ && $demo[i+3] == ‘v’ )
{

$str.=‘<div’;
$n = 1;
while($demo[i+3+n] != ‘>’)
{
$str.=$x[i+3+n];
$n=($n+1);

}

$str.=‘>’;
$i= ($i+3+$n+1);
$str.=’
';
}

}

$Handle = fopen(‘Div/’.$name[0].“.txt”, ‘w’) or exit(“Unable to open file!”);
$Data = $str;
fwrite($Handle, $Data);
fclose($Handle);
echo $str;
echo “hi”;
?>

I noticed a strange thing 'the loop did not display any data on screen but when i added a character in the loop for display, the data was displayed on the screen along with the character repeated multiple times.

Can anyone help me?

I think you need to take a step back help us understand why your are doing this in the first place. IMO, this is a kooky approach.

For example, you are reading from a file and then immediately writing the same values back. Then reopening it and parsing it, when you already have it assigned to an existing variable ($current). Then looping through it again. Jeez, one loop should be plenty.

Hi Thanks for your reply ,you are right I could have taken it directly from current variable.But leaving that aside I’ll explain you now what I am trying to do:

I am trying to fetch html data from a page and store it in a file in the Html folder on the server.Please note I am able to get the whole data of the html page stored in a file in the Html folder.

After that I want to store store another file which has only the ‘<div>’ tags of the desired html page only in another file in the Div folder on the server I need it for some futher processing for any business logic.

I am not able to produce contents in the div tag file from my logic above it is generating empty file please can you help me where I am going wrong as I am naive in php.

Thanks for your time, appreciable :slight_smile:

Take a look at the [fphp]strip_tags[/fphp] function.