note 74343 deleted from function.preg-replace by nlopess
| From: | nlopess@php.net | Date: | Wed, 11 Apr 2007 11:48:39 +0000 |
| Subject: | note 74343 deleted from function.preg-replace by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-125286@lists.php.net to get a copy of this message | ||
Note Submitter: tomato at telenet dot be
----
This might seem really obvious for most programmers, but you can use preg_replace to echo out some
nifty errormessages to the users, using CSS.
Example:
<?php
// Start an object containing your HTML page
ob_start()
?>
<html>
<head>
<title>Cookiemonster simulator</title>
</head>
<body>
<h1>Welcome to the cookiemonster simulator!</h1>
<form>
<p>blah blah blah</p>
</form>
</body>
</html>
<?php
// Get the HTML page into its own variable.
$html = ob_get_contents();
ob_end_clean();
// Some stuff to check the form here...
if 'something is wrong' {
$error = 'You killed cookie monster!. ';
$html = preg_replace('/<body>/', '<body><p
class="error">' . $error . '</p>', $html);
unset($error);
}
// Echo out the form
echo $html;
?>
Splitting up your HTML and your PHP also has the advantage that WYSIWYG-editors, like Dreamweaver
and Frontpage, can still read the HTML without messing up your PHP.
BTW: For more advanced formhandeling I strongly suggest fillInFormValues() : Written by Gavin
Andresen that can be found on www.onlamp.com/pub/a/php/2006/03/16/autofill-forms.html