#native_company# #native_desc#

10 Easy Solutions for PHP String Manipulation

By W. Jason Gilmore
on June 2, 2010

PHP sports a massive string manipulation library, offering almost 100 native functions capable of dicing, splicing, parsing and searching text in every imaginable fashion. In fact, PHP’s capabilities in this regard are so strong that it can sometimes be difficult to determine the best possible approach for accomplishing a particular string-related task. In this article I highlight the ideal solutions to 10 common string manipulation tasks.

1. Determining the Length of a String

This is by far the most obvious example demonstrated in this article, but the question of how to determine the length of a string comes up so often that I’d be remiss to neglect mentioning the strlen() function:

$text = "sunny day"; $count = strlen($text); // $count = 9

2. Truncating Text to Produce a Summary

News-oriented websites often produce summaries of recently published articles by truncating each article after the first 200 or so characters and adding an ellipsis at the end of the truncated string. You can achieve this effect using the substr_replace() function. For reasons of space I’ll demonstrate this effect using a 40-character limit:
$article = "BREAKING NEWS: In ultimate irony, man bites dog."; $summary = substr_replace($article, "...", 40); // $summary = "BREAKING NEWS: In ultimate irony, man bi..."

3. Counting the Number of Characters and Words in a String

You’ll often see blogs and news-oriented websites summarizing the total number of words in a given article, or requiring contributors to submit articles falling within a predefined range. You can report an article’s size in terms of word count using the str_word_count() function:
$article = "BREAKING NEWS: In ultimate irony, man bites dog."; $wordCount = str_word_count($article); // $wordCount = 8
Sometimes you’ll need to exert even tighter control over the amount of space used for contributions such as comments, tracking them to the character. If you’d like to know how many characters comprise a string, see the function count_chars().

4. Parsing a CSV File

Data is commonly stored in files using a comma-separated format (known as CSV format). CSV uses a comma or similar predefined delimiter character to separate each column comprising a row. You’ll often create PHP scripts capable of importing this data and parsing or manipulating it to your needs. Over the years I’ve seen quite a few different approaches to parsing a CSV file, most notably using a combination of the fgets() and explode() functions to read and parse the file. However, the easiest solution involves using a function that isn’t actually classified as part of PHP’s string manipulation library: fgetcsv(). This function gives you the ability to exert greater control over both the delimiter character and any enclosure character used to formally delimit each string.
Consider the following CSV file:

1,John,Smith,Plumber 2,Mark,Seagal,Instructor 3,Peter,Haines,Writer

Using fopen() and fgetcsv(), we can easily parse this file to retrieve the first name of each contact:

$fh = fopen("contacts.csv", "r"); while($line = fgetcsv($fh, 1000, ",")) { echo "Contact: {$line[1]}
"; }

5. Converting an Array into a String

At some point, you will probably need to create CSV files as well as read from them, meaning you’ll need to convert data into comma-delimited strings. If this data were initially retrieved from a database, it’s likely to be made available to you as an array. You can convert an array to a comma-delimited string using the implode() function:

$csv = implode(",", $record);