# PHP Strings

Posts about manipulating strings in different ways in PHP

## Display A Dynamicly Highlighted String With PHP

7th February 2011 - 4 minutes read time

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 - 3 minutes read time

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 - 3 minutes read time

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.

## Palindromes In PHP

1st June 2010 - 4 minutes read time

Richard Wiseman is a psychologist, magician, and author who runs a little blog over at http://richardwiseman.wordpress.com/. His blog talks about all sorts of things, but every Friday he posts a little puzzle that you can have a go at solving.

The last puzzle posted talked about palindromic numbers and speed, here is the puzzle in full.

The other day I went for a bike ride. My favourite route has signs every meter saying how far you have travelled. I came across the sign saying '15951 meters' and thought 'Oh, that's interesting, it is a number palindrome because it reads the same from left to right as right to left'. Then I rode on. Two hours later I came across the next palindromic number sign. How fast was I going?

## Format A List Of Items In PHP

8th January 2010 - 7 minutes read time

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.

The function takes a single parameter, which can either be an array or a comma separated string. If an array is passed to the function then it is converted into a comma separated string and then passed onto the next part in the function. The function then removes any trailing commas, any commas that have nothing in between them and then makes sure that each comma has a single space after it. The final step is to replace the last comma with the word "and". Once the manipulation is complete then the resulting string is returned. If the string (after removing any trailing commas) doesn't contain any commas then it is simply returned.

## Extract Keywords From A Text String With PHP

29th July 2009 - 5 minutes read time

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.

The first thing the function defines is a list of "stop" words. This is a list of words that occur quite a bit in English text and would therefore interfere with the outcome of the function. The function also uses a variant of the slug function to remove any odd characters that might be in the text.

## Using PHP implode() To Construct Strings

4th June 2009 - 4 minutes read time

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.


$array = array(1, 2, 3, 4, 5, 6); echo implode(',',$array);

This will print out the following:


1,2,3,4,5,6

The good thing about the implode() function is that it doesn't add stray commas to the start and end of the string so there is no need to alter the string after the function is used.

## Disemvoweling PHP Function

7th April 2009 - 2 minutes read time

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.

function disemvowel($string) { return str_replace(array('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'), '',$string);
}

As an example, the first sentence on this post:

Disemvoweling is a technique used on blogs and forums to censor any post or comment that contains spam or other unwanted text.

would appear like this:

## Preparing HTML And PHP Code For Pubilishing On Websites

1st April 2009 - 4 minutes read time

I talked a while ago about Adding Code To Wordpress Blogs And Comments, but I decided that it needed a bit of code to do this automatically.

So here it is, prepared by the text processor.

## Find File Extension In PHP

23rd February 2009 - 2 minutes read time

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.

$ext = substr(strrchr($fileName, '.'), 1);

This code can be used in the following way.

$fileName = '\path\to\file\afile.wibble';$ext = substr(strrchr($fileName, '.'), 1); echo$ext;

The output here is 'wibble';