Currently seeking new career opportunities in web development, particularly with Laravel, Hire Me

PHP's extract function and HTML arrays

David Carr

Tutorials PHP & MySQL

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.

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.