Fri, 17 May 2024


Assorted PHP Functions

By: Peter, NetArt Media
Wed, 1 August 2018
Assorted PHP Functions

Having so many built-in functions within the language of PHP is something we should all take advantage of as much as possible. There are many that will seldom be used, but the 4 mentioned in this article are functions that I personally use on a regular basis. If you plan on building scripts and content management systems you may find yourself using them as well.

Let's start with the 'wordwrap' function. This works well within website layouts. Perhaps someone has a CSS div and they want to limit the amount of characters per line contained within it. This isn't always a perfect solution, but if the person has a good idea of the average amount of characters that will be placed inside the area, it will work fine.


The result...

This is a
string I want
to break up
according to an
amount of
characters.

'15' represents the amount of characters to be returned per line. Simply change that to get your desired result.

Next up, we have the 'strip_tags' function. This strips out any HTML tags from a string or variable. Many times, security can depend on something as simple as this function. Allowing links that come from the public in comment sections and other areas within a server usually isn't good practice. Sometimes the links can lead to dangerous pages or scripts. So, stripping all the tags out can be a good option.


And the result...

Bad Link

As you can see, all HTML tags have been stripped from the string. This includes the URL itself. Now, there are ways around this, in the event you want the URL to still be returned. That is something I can go into at a later date.

Slashes can be both a security concern, as well as a pain in the butt when dealing with content that has been saved to a database perhaps, or just strings which contain this sometimes unwanted character. 'strip_slashes' can take care of it for us.


The result...

We don't want any slashes.

You could also use additional functions within the above code block, in the event you're dealing with more than just slashes. In the case of stripping out slashes, however, this works great.

The 'strrev' PHP function is very handy and something I have been known to use quite frequently from time to time. It can add extra security to personal passwords that might be saved to a file perhaps, or even be utilized for scrambling captcha codes. It isn't foolproof by any means, but it can be handy when used in conjunction with other security measures. Reversing a string could work in many scenarios.


The result for the above code block in a browser is...

gnirtS desreveR

So there we have them. 4 more built-in PHP functions which can be used for practical and security purposes. They are usually used with other functions to get the most desirable results, but even by themselves can be utilized for simple outcomes.

I hope this article helps you in some way and thanks for dropping by.


Category: Web Development
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox