PHP's extract function and HTML arrays

David Carr

1 min read - 30th Oct, 2013

I'm a huge fan of using the extract function in php its very handy it lets you extract an array of keys to variables.

For example when submitting a form normally I add each post key to a matching variable:

$postTitle = $_POST['postTitle'];
$postcat = $_POST['postcat'];
$postCont = $_POST['postCont'];

That's fine for small forms but when working on larger forms it gets very tedious quickly that's where the extract function comes in instead of having to add each post key to a variable I pass the $_POST to extract which results in the same thing:

extract($_POST);

//I can now call any key by its variable name
echo $postTitle;

This is really convenient, recently I've discovered one drawback when working with HTML data in a form and using extract and closing slashes are removed from the tags take this:

$postCont =" <h1>My Title</h1><p>some content etc</p>";

When passed to extract the result ends up missing the h1 tags

$postCont =" <h1>My Title<h1><p>some content etc<p>";

That's not good at all! I've experimented a lot there doesn't seem to be a workaround so when working with HTML in your forms bind the POST item to a variable as you would normally do, it will avoid the slashes being removed.

0 comments
Add a comment

Copyright © 2024 DC Blog - All rights reserved.