note 74343 added to function.preg-replace
| From: | tomato at telenet dot be | Date: | Fri, 06 Apr 2007 13:49:15 +0000 |
| Subject: | note 74343 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-125145@lists.php.net to get a copy of this message | ||
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
----
Server IP: 193.190.198.20
Probable Submitter: 213.224.83.20 (proxied: 81.165.80.45)
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- https://master.php.net/note/edit/74343
Del: integrated -- https://master.php.net/note/delete/74343/integrated
Del: useless -- https://master.php.net/note/delete/74343/useless
Del: bad code -- https://master.php.net/note/delete/74343/bad+code
Del: spam -- https://master.php.net/note/delete/74343/spam
Del: non-english -- https://master.php.net/note/delete/74343/non-english
Del: in docs -- https://master.php.net/note/delete/74343/in+docs
Del: other reasons-- https://master.php.net/note/delete/74343
Reject -- https://master.php.net/note/reject/74343
Search -- https://master.php.net/manage/user-notes.php