TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack (3) Thread Tools Display Modes
Old 01-07-2008, 08:21 PM   3 links from elsewhere to this Post. Click to view. #1 (permalink)
The Wanderer
 
vujsa's Avatar
 
Join Date: Dec 2007
Location: Indianapolis, Indiana, USA
Posts: 16
Thanks: 0
vujsa is on a distinguished road
Default How Would I Apply htmlentities() To Every Array Item

Well, I don't know why I can't figure this out. It should have taken a few minutes for me to do but I had a lot of trouble with my broadband connection last night while I was working on it and I think that my frustration has gotten the better of me.

By the way, the likelihood of me figuring this out after I explain it here is rather high. lol

Simply put, I want to apply the function htmlentities to every item in an array even if I don't know how many items in the array or more importantly, the names of the array keys. However, the task must maintain the array key names when finished since the keys are used later to process the data.

Basically, I'm working on a generic script that would allow me to use the same script over and over for many different HTML forms. Since this script just acts as an in between script that cleans the data before it is passed to the next step, I need it to maintain all of the array information except for modifying the values as needed.

Okay, so why not just clean each item as it is processed later?
Well, the sooner I clean the data, the smaller the chance of a problem. The next step of the process is where my biggest concern is as it provides the largest security concern.

In addition to the htmlentities function, I'll be running a few more data cleaning functions on the input to ensure the safety of the system.

Assume the following array type to be used:
Code:
$_POST['f_name'] = "John";
$_POST['l_name'] = "Doe";
$_POST['username'] = "jdoe";
$_POST['email'] = "jdoe@nowhere.bfe";
Thanks for the help.
vujsa
__________________
Need PHP Help? - Handy PHP
vujsa is offline  
Reply With Quote
Old 01-07-2008, 08:27 PM   #2 (permalink)
The Gregarious
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 563
Thanks: 15
Village Idiot is on a distinguished road
Default

Foreach is good for looping though arrays, PHP: foreach - Manual

For your specific problem, to go though all POST data would be

PHP Code:
foreach($_POST as $post)
{
  
$post =  htmlentities($post );

__________________
There are two ways to write bug-free code, only the third one works.
Village Idiot is offline  
Reply With Quote
Old 01-07-2008, 08:38 PM   #3 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 265
Thanks: 2
TlcAndres is on a distinguished road
Default

PHP Code:
$_POST array_map('htmlentities',$_POST); 

array_map goes into every array element and preforms the function be it user made or from the system.
TlcAndres is offline  
Reply With Quote
The Following User Says Thank You to TlcAndres For This Useful Post:
sjaq (01-07-2008)
Old 01-07-2008, 11:59 PM   #4 (permalink)
The Wanderer
 
vujsa's Avatar
 
Join Date: Dec 2007
Location: Indianapolis, Indiana, USA
Posts: 16
Thanks: 0
vujsa is on a distinguished road
Default

Fantastic! That was exactly what I needed. I wish there was a way to search the PHP site by what you want to do instead of reading through page after page of non-related information.

I tried to use foreach prior to posting without any luck. This works and is much quicker than any of the loops.

Thanks,
vujsa
__________________
Need PHP Help? - Handy PHP
vujsa is offline  
Reply With Quote
Old 01-08-2008, 12:07 AM   #5 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 218
Thanks: 16
RobertK is on a distinguished road
Default

Quote:
Originally Posted by vujsa View Post
Fantastic! That was exactly what I needed. I wish there was a way to search the PHP site by what you want to do instead of reading through page after page of non-related information.
There is a way; if you go to the online manual, the HTML copy, and enter a function into the search box you can find it. Otherwise you're stuck browsing the manual sections to find array functions. Not a bad idea, but I know what you mean.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Old 01-08-2008, 12:15 AM   #6 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 265
Thanks: 2
TlcAndres is on a distinguished road
Default

Well just in case you'd like another method.

PHP Code:
foreach($_POST as $key => $val)
{
   
$_POST[$key] = htmlentities($val);

TlcAndres is offline  
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/advanced-php-programming/1886-how-would-i-apply-htmlentities-every-array-item.html
Posted By For Type Date
Why not use a foreach loop instead of having a function called array_walk? - comp.lang.php | Google Groups This thread Refback 01-08-2008 11:42 AM
Why not use a foreach loop instead of having a function called array_walk? - php.general | Google Groups This thread Refback 01-08-2008 09:42 AM
Why not use a foreach loop instead of having a function called array_walk? - es.comp.lenguajes.php | Grupos de Google This thread Refback 01-08-2008 03:48 AM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 04:44 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0