Hi,

Code below doesn't work for a file contains HTML content? How do we solve this issue?

Result must be <span but it dispays blank screen.

Thanks in advance

Content of index.txt

<span class="mw-headline" id="Zero"><a href="/wiki/Zero" title="Zero">Zero</a></span></h2>

PHP to read the file

<?php
$handler = fopen('index.txt','r');

while ($line = fgets($handler))
{
    echo substr($line, 0, 4); exit;
}

fclose($handler)
?>
Member Avatar for diafol

Well it probably will display a blank screen '<spa' shouldn't give any screen output. What about browser view source?

Is this the code you want to run? Or is it for testing purposes? As the 'while' makes little sense if you're exiting after the first line.

BTW - if you have a blank line at the beginning of the file, it will show a blank.

I think it will never work when I try to echo html code in a page unless trying to echo it into textarea etc.

I'm exiting because the txt file many lines so I was just testing with one line only.

In browser code view, <spa is being printed correctly.

syntax

<?php $handler = fopen('index.txt','r');
while ($line = fgets($handler)){ echo '<br>'.htmlentities($line);}
fclose($handler); ?>

the original works, without the substring and exit, to generate functional html,
this version generates displayable html

I'll test it on Monday. Thanks

htmlentities($line);

Works fines thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.