PHP htmlentities not working

If you find yourself in a situation where htmlentities() function is not working, don’t be alarmed there is a fix. Here’s what I found through my research:

Let’s take a french character “è” and convert it to an htmlentity:

If you print the output it will look exactly what you started with. Now, if you apply the strtoupper() function to the same variable before printing it, the character will no longer be decoded by the browser and will display html entity code, like this: è

This happens because the HTML friendly code is case sensitive, be careful when doing this. One of many alternatives is to use CSS to transform text to upper case.

PHP Get Object Vars / PHP Get Object Methods

Have you written an application and wanted to see if the result set from your database request returned the correct results?

Have you worked with someone else’s code and had to use an object but wasn’t sure what kind of variables and methods that object contains?

Well now you can check all of that (class properties/variables, class methods/functions and their values).

PHP’s function print_r() will allow you to view properties and values of the following: array, object, variables.

The function var_dump() allows you to do exactly the same thing, except; this function will output results in the browser’s console, tell you the variable’s type and its values.

The previous two functions allow you to view only properties and their values, but what if you want to know which functions/methods the class has available at its disposal? In that case, use get_class_methods() which will output exactly what you want.

To wrap this up, use print_r(), var_dump(), and get_class_methods() for your testing and exploration needs.

strtolower() – string to lower case

Now and then you will want to convert your string to lower case. At times when using associative arrays, I prefer to use lower case strings because it’s easier to remember and a good practice to follow the same coding standard, also associative arrays are case sensitive.

The function strtolower() accepts one parameter (string) and returns lower case string on success.

Example:

Simple but powerful.

Uploaded File – Check File Type (PHP)

Recently, I found an issue with checking a file’s type while using the simple upload form. After some investigation I found why the code for checking a file’s extension type wasn’t working as well as an alternative to solve the problem.

Here’s the code for the form I used:

For simplicity, I got this sample of PHP code from here.

Now, usually I would use the $_FILES array to test a file’s type during processing to decide whether to accept or reject the file but the $_FILES[“filename”][“type”] result (even though correct) would not respond to my request as planned.

The hint (in regards to $_FILES[“filename”][“type”]) I found was on php.net website which states the following: “The mime type of the file, if the browser provided this information. An example would be “image/gif”. This mime type is however not checked on the PHP side and therefore don’t take its value for granted.”

So, an alternative solution to this is to retrieve the file’s full name including extension using $_FILES[“filename”][“name”], explode the name using a period ( . ), and lastly use the last piece of array (which is the resulting extension) to match it with what you want. Here’s a sample of the working code:

I hope this helps. Enjoy it.

NetBeans 7.3 – refresh or update folders/files in a project

NetBeans has a background scanner which (from time to time) scans your folders and updates newly added files to your project. There are times when you want to force NetBeans to update folder files instead of waiting for the scheduled scan.

To perform this follow these steps:

1. Click Source in the top menu.
2. Select “Scan for External Changes

It might take a little bit of time but this will refresh your folders with updated contents.