PHP Strings

Posts about manipulating strings in different ways in PHP

PHP Logo

Randomising The Middle Of Words In PHP

18th November 2008 - 3 minutes read time

I was sent an email the other day that contained some text were the start and end letter of each word were left alone, but the middle of each word was randomized. The weird part was that the text was still readable, which is due to the way in which the brain processes words.

I wondered if I could replicate this using a PHP script. All I would need to do is split apart the sentence into the component words and loop through those words, randomizing the middle of them. Clearly, it is not possible to mix up the order of letters in a word less than four characters long so a check would be needed for this. This is what I cam up with:

PHP Logo

Simple Swear Filter In PHP

30th September 2008 - 2 minutes read time

Use the following function to filter out words from user input. It works by having a pre-set array of words that are to be excluded, this array is then looped through and each item is used to replace any instances of that word within the text. The regular expression uses the \b character class, which stands for any word boundary. This way you don't get the middle of words being filtered out when they are not meant to be.

By using the e of the preg_replace function it is possible to run PHP functions within the output. In this case we count the number of characters found in the replace and use this to create a string of stars (*) of equal length.

PHP Logo

What To Do When get_html_translation_table() And htmlspecialchars() Doesn't Work

17th September 2008 - 1 minute read time

I found a little problem today when processing a bit of text from a non-english site. I found that the text was being loaded properly, but because it was in UTF-8 encoding PHP couldn't use htmlspecialchars() or apply get_html_translation_table() to the string to properly encode the foreign characters. These methods just don't have any effect. This is because PHP (before version 5.2.x) doesn't natively support unicode character encoding and is therefore not able to translate characters in UTF-8 format.

To get around this just use the utf8_decode() function on the string to convert it into a usable format.

PHP Logo

String Equals Zero In PHP

8th September 2008 - 4 minutes read time

Due to the weakly-typed nature of PHP you can do some odd things, some of which are good, and some of which will enable you to shoot yourself in the foot. Take the following little snippet.

echo '1' + 5;

In some languages this might cause the program to fall over, but PHP will try to evaluate any string into an integer. In this case it converts the string to an integer 1 and adds this to 5 to make 6.

As an aside, if you did this in JavaScript then you would find the opposite result. Because the concatenation character is the same as the addition character JavaScript will always try to truncate the value if any of the present values are a string. So the result in JavaScript would be "15".

PHP Logo

Using PHP To Split A String Into Characters

2nd September 2008 - 2 minutes read time

Use the following code to split a string into an array of characters.

$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);

It uses the preg_split() PHP function which takes a number of parameters. These area as follows:

PHP Logo

Using The e Modifier In PHP preg_replace

20th August 2008 - 3 minutes read time

The PHP function preg_replace() has powerful functionality in its own right, but extra depth can be added with the inclusion of the e modifier. Take the following bit of code, which just picks out the letters of a string and replaces them with the letter X.

PHP Logo

Tidy Up A URL With PHP

4th August 2008 - 3 minutes read time

Lots of applications require a user to input a URL and lots of problems occur as a result. I was recently looking for something that would take a URL as an input and allow me to make sure that is was formatted properly. There wasn't anything that did this so I decided to write it myself.

The following function takes in a URL as a string and tries to clean it up. It essentially does this by splitting is apart and then putting it back together again using the parse_url() function. In order to make sure that this function works you need to put a schema in front of the URL, so the first thing the function does (after trimming the string) is to check that a schema exists. If it doesn't then the function adds this onto the end.

PHP Logo

Clean Up A Comma Separated List In PHP

1st August 2008 - 2 minutes read time

Getting users to enter a list of items is a normal practice, but getting users to do it properly is sometimes a challenge in itself. Some users will put spaces in between the commas, others will not, some will even put a trailing comma at the end of the list.

Here is some code that can be used to clear up a comma separated list using some simple regular expressions. It works using the preg_replace() function, and by passing this function an array of options patterns and an array of replacements.

PHP Logo

Fastest Way To Match String With PHP

10th July 2008 - 5 minutes read time

There are many ways to find a string within another string using PHP, but which one is the quickest? I did the same sort of test that I have done with double or single quotes and downloading web pages with a few of the string matching functions available in PHP.

I first generated a very long string that could be used in the string matching functions. The following for loop generates a string containing "iteration 0 and iteration 1 and iteration 2..." right up until "iteration 4999 and".

PHP Logo

Shortening Long URLs With PHP

13th June 2008 - 1 minute read time

Print out a full URL for a link will sometimes mess up your formatting, especially if you URL is quite long. This might be the case if you are linking to a Google search page, or have an automated script that shows numerous URLs of indeterminate length. The following function will reduce any URL longer than 45 characters by splitting it in two and join them up with a simple string.