PHP Strings

Posts about manipulating strings in different ways in PHP

Display A Dynamicly Highlighted String With PHP

7th February 2011

This function might be of limited use, but it can create some neat effects in your titles. It works by splitting a string into little bits using the spaces and then puts it back together again into two sections. The first section will be normal, but the second section will be wrapped in a span element. By using this function you can create an interesting effects in your titles by styling the first half differently from the second.

PHP Random String Function

28th January 2011

I was testing a string manipulation function today (which I will post some other time) and I wanted to create a random string of characters that I could feed into it, so I came up with the function below. I thought it was a neat use of the rand() and chr() PHP functions, so here it is.

PHP Paragraph Regular Expression

9th June 2010

I quite often find the need to extract a section of text from the beginning of a blog post or similar to be used as the excerpt. I normally use a function that will count the number of whole words available and return the string containing those words.

A good alternative to this, although only applicable if the original post is in HTML, is to use a regular expression to extract the contents. The following code will take a string and extract just the first paragraph of text.

Format A List Of Items In PHP

8th January 2010

It is usual when writing a list of items to separate each item with a comma, except the last two items, which are separated with the word "and". I recently needed to implement a function that took a string and converted it into a list of this type so I thought I would expand on it and post it here.

Extract Keywords From A Text String With PHP

29th July 2009

A common issue I have come across in the past is that I have a CMS system, or an old copy of Wordpress, and I need to create a set of keywords to be used in the meta keywords field. To solve this I put together a simple function that runs through a string and picks out the most commonly used words in that list as an array. This is currently set to be 10, but you can change that quite easily.

Using PHP implode() To Construct Strings

4th June 2009

If you are constructing a simple string from a set of variables contained in an array then you can use the implode function to convert the array into a string. The implode() function takes two parameters. The first is the glue that is used to join the items in array together and the second is the array to use. Here is a trivial example of implode() in action.

Disemvoweling PHP Function

7th April 2009

Disemvoweling is a technique used on blogs and forums to censor any post or comment that contains spam or other unwanted text. It involves simply removing the vowels from the text so that it is almost, but not entirely, unreadable.

Use the following function to disemvowel a string of text.

Find File Extension In PHP

23rd February 2009

This simple code example uses a combination of strrchr to find the last occurrence of a string and substr to return part of the string in order to find the file extension for a given filename. This is ideal if you want to quickly find a file extension.