I have a couple of PHP scripts, one that allow a text file to be edited, the other that reads the text file line by line, makes some text replacements and includes the text within a web page. My web page uses UTF8 encoding but the text file appears to be ANSI. This isn’t a problem with plain text but it is with special characters. Any suggestions as to how I square this circle, please? Thanks!
This is my PHP:
<?php
$file = 'news.txt';
if ( $_POST['submit'] == 'Submit') {
$message = $_POST['message'];
file_put_contents($file, $message);
echo '<p>The news has been updated.</p>',"\
";
} else {
$news = file_get_contents($file);
?>
<p>To change the news page simply amend the text in the box below.</p>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<textarea name="message" id="message" cols="100" rows="25"><?php echo $news; ?></textarea>
<br />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
}
?>
and
<?php
$file = 'news.txt';
if ( file_exists($file) ) {
$paras = file($file);
foreach ( $paras as $para ) {
$para = trim($para);
$para = htmlspecialchars($para);
$para = preg_replace('#\\*{2}(.*?)\\*{2}#', '<strong>$1</strong>', $para); // bold
$para = preg_replace('#\\/{2}(.*?)\\/{2}#', '<em>$1</em>', $para); // italics
$para = str_replace('£', '£', $para);
if ( substr($para, 0, 2) == '#3' )
echo '<h3>',substr($para, 2, strlen($para)-1),'</h3>',"\
";
elseif ( substr($para, 0, 2) == '#4' )
echo '<h4>',substr($para, 2, strlen($para)-1),'</h4>',"\
";
else
echo '<p>',$para,'</p>',"\
";
}
}
?>