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
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack (3) Thread Tools Search this Thread 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)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
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 );

__________________

Village Idiot is offline  
Reply With Quote
Old 01-07-2008, 08:38 PM   #3 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
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: 217
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: 264
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
Old 01-09-2009, 01:18 AM   #7 (permalink)
The Visitor
 
Join Date: Jan 2009
Posts: 3
Thanks: 0
drolex is on a distinguished road
Default

Quote:
Originally Posted by TlcAndres View Post
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.
Is there a way to use the optional parameters of htmlentities such as quotestyle and character-set parameters?
drolex is offline  
Reply With Quote
Old 01-09-2009, 01:40 AM   #8 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I don't understand your question, drolex. It is possible to use them, but for what purpose? What do you want to achieve?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-09-2009, 02:42 AM   #9 (permalink)
The Visitor
 
Join Date: Jan 2009
Posts: 3
Thanks: 0
drolex is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
I don't understand your question, drolex. It is possible to use them, but for what purpose? What do you want to achieve?
The default quotestyle is ENT_COMPAT which only encodes double quotes. I think I need ENT_QUOTES which will also encode single quotes. I wanted to use character set "UTF-8" because everything else is UTF-8, and the default for htmlentities is ISO-8859-1 - Western European.

I have a database that stores descriptions for things. The user often enters descriptions containing quotes. All quotes and symbols need to be converted to html entities so the description can be show and not mess up the rest of the page. I want to store the descriptions pre-encoded so it doesn't have to use extra process cycles everytime the page is shown.
drolex is offline  
Reply With Quote
Old 01-09-2009, 08:40 PM   #10 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

Well the first and most obvious method to be would be to use create_function()

PHP Code:
<?php
$_POST 
array_mapcreate_function('$string''return htmlentities($string,ENT_QUOTES);'), $_POST );
Enfernikus is offline  
Reply With Quote
Old 01-09-2009, 09:33 PM   #11 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Whats wrong with just using a foreach loop?
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 01-10-2009, 01:28 PM   #12 (permalink)
The Contributor
 
Join Date: Jan 2009
Posts: 40
Thanks: 10
Scottymeuk is on a distinguished road
Default

Or what you could do is:


PHP Code:
function get_post($string)
{
    return 
htmlentities($_POST[$string]);

and then, to use it you do

PHP Code:
get_post('john'
For example. It needs to be extended a lot but its the basics of it.
Scottymeuk 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 Search this Thread
Search this Thread:

Advanced Search
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 03:37 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design