PHP Strings

Posts about manipulating strings in different ways in PHP

Preparing A URL With PHP

12th June 2008

There might be many instances where you will create a program in PHP that takes a URL as input and does something with the address. This might be a site analysis or an image resize, but whatever the use is, you need to be sure that the URL will work or at least has the same format.

Getting An Excerpt Of Text With PHP

1st June 2008

I have talked before about truncating strings to include whole words, but what if you want to have complete sentences? The following function will truncate a string the number of words specified and until the sentence ends.

Transform Text Links Into Anchors

28th May 2008

Using the strip_tags() function on any user generated input should be common practice in order to stop users putting in odd bits of HTML and messing up your nicely coded HTML. You might want to allow anchor tags to be used, which is fine, but some users might use this to include onmouseover and other events in order to run JavaScript annoyances on the page.

So after stripping all tags from your text you can then run the following function to turn any links into anchor elements. This will work with any plain text that you want to use it on.

Turning ASCII Text Into HTML With PHP

25th April 2008

Providing a text box for users to type information in is very common, but usually people want to include line breaks and links with the text and they expect the site to lay it out just as they had intended it. The following function will turn any ASCII text string into the approximate HTML equivalent.

Cut A String To A Specified Length With PHP

17th April 2008

Cutting a string to a specified length is accomplished with the substr() function. For example, the following string variable, which we will cut to a maximum of 30 characters.

Creating A URI Slug With PHP

28th March 2008

The use of mod_rewrite on a site can have a powerful effect on search engine positioning, but to do it properly you will need to create a "slug" for each page. A slug is a lowercase alphanumeric version of the page title, with any spaces removed.

To get a slug you will need to use a function to turn a readable page title into a string that can be used as part of a URI.