Lateral Code
A Web Development Blog Focused on Code and Technology
Currently Browsing: Home » How to Quickly Apply Many PHP Functions
How to Quickly Apply Many PHP Functions
By Karthik Viswanathan
Let’s say that you have just created a new company and are in need of a contact form. For this form, you want to let the user provide a name, e-mail (so you can get back to them), and a message. In order to make sure you are receiving valid content, it is vital that you check and manipulate user input in order to meet your needs.

Requirements
For example, what if you wanted the message to be in a strict format, like so:
Every Word In A Sentence Must Be Capitalized. There Must Be No Html Tags And The Rest Of The Letters Have To Be Lower Case. In Addition, No Excess Whitespace Must Exist On Each End Of The String.
There are various PHP functions to accomplish the above task. In fact, you could just use:
$str = trim( ucwords( strtolower( strip_tags( $str ) ) ) );
to achieve the aforementioned output. Unfortunately, this looks quite odd and bulky. Furthermore, you would have to use a similar structure for another long list of string manipulations.
Solution
In today’s post, we present a way to quickly apply many PHP functions. Indeed, we will actually write a single function that should make your life much easier. Best of all, it is very easy to create!
Going back to our original problem, notice how each function takes in exactly one parameter. In addition, we are storing the value back inside the variable once completed. Why not just have a function that takes a string parameter? This string will contain general PHP functions that only require one parameter:
trim|strtolower|ucwords|strip_tags
The PHP Function
We can also pass a variable containing our String. Then, using PHP’s eval function, we can create a simple way to apply many php functions with ease:
function apply( &$var, $list ) {
$functions = split( '\|', $list );
if( is_string( $var ) ) {
foreach( $functions as $change ) {
eval( '$var = ' . $change . '("' . $var . '");' );
}
}
}
In the above code, we first find all the functions and then loop through each one, applying the call as we go. eval will take the PHP statement specified in quotes and actually parse it as real PHP. Thus, we use normal syntax.
Conversely, nlog pointed out that it is also possible to use:
function apply( &$var, $list ) {
$functions = split( '\|', $list );
foreach( $functions as $change ) {
$var = $change( $var );
}
}
The above solution can be applied to variables of any type. In addition, it is more straightforward.
For those of you who are wondering, notice that this function does not return anything. It also has a parameter with an & in front of it (&$var). In simple terms, this symbol enables the function to gain access to the reference of $var instead of just its value. Thus, if $var is changed inside the function, it will also be altered in the outer program. Consequently, when calling this function, we do not need to worry about the output, as it is directly stored in our variable:
$str = " heLLO HoW ARE you? ";
apply( $str, 'trim|strtolower|ucwords|strip_tags' );
echo "'" . $str . "'";
Final Result
The above code would output:
'Hello How Are You?'
That’s all for now, folks!
Tags: php
This entry was posted on Sunday, September 27th, 2009 at 20:33:15. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
7 Responses to “How to Quickly Apply Many PHP Functions”
Leave a Reply
Great functions. Have you checked the time taken between your function and normal way using functions.
@scvinodkumar: I haven’t related the execution of the two methods, but I’m pretty sure there should be a negligible time difference.
Wov, very useful tnx for share!
Now with 100% less eval, just as nasty: http://codepad.org/3POY6AJ4
@nlog: Thanks for your input. I wasn’t aware that PHP supported
$change( $var ). I’ll add that information in the post.Stumbled across this post again and thought it might be interesting to see how it could be done in another language.
http://codepad.org/bIMsKgLh
I change it to class, better performance:
http://www.oulan.com/w/2011/03/15/php-apply-functions/
ps. Blog is written in Chinese.