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!