String manipulation is probably one of PHP's biggest strengths, as it offers a handful of functions to perform a variety of operations. Even when almost every need can be fulfilled by using PHP's core methods, some forms of string manipulation may prove troublesome.
To find out more about some of PHP's core string methods see http://php.net/manual/en/ref.strings.php.
CakePHP offers a utility class named String
to help us deal with strings. This recipe introduces the class and its few, yet useful set of methods.
We need a controller to use as placeholder for our code. Create a file named examples_controller.php
and place it in your app/controllers
folder, with the following contents:
<?php class ExamplesController extends AppController { public $uses = null; public function index() { $this->_stop(); } ?>
Edit the app/controllers/examples_controller.php
file and add the following at the beginning of the index()
method:
$lines = array( '"Doe, Jane", [email protected]', '"Doe, John", [email protected]' ); foreach($lines as $i => $line) { $line = String::tokenize($line, ',', '"', '"'),
$line = array_combine(array('name', 'email'), $line); foreach($line as $field => $value) { $line[$field] = preg_replace('/^"(.+)"$/', '\1', $value); } $line['id'] = String::uuid(); $lines[$i] = $line; } foreach($lines as $line) { echo String::insert('[:id] Hello :name! Your email is\: :email', $line) . '<br />'; }
If you now browse to http://localhost/examples
you should see a text output similar to the following:
[4d403ee1-6bbc-48c6-a8cc-786894a56bba] Hello Doe, Jane! Your email is: [email protected]
[4d403ee1-9e84-487f-95cf-786894a56bba] Hello Doe, John! Your email is: [email protected]
The String
class offers the following methods for string manipulation:
cleanInsert()
: Cleans a string generated via the String::insert()
method.insert()
: Replaces variable placeholders in a string with a set of values.tokenize()
: Separates a string into parts using a given separator, and ignoring the separator instances that appear between the specified bound strings.uuid()
: Returns a random UUID string.This recipe starts by defining an array of two strings, each of them following a format similar to what we would find on a CSV (comma-separated values) file. For each of those lines, we use the String::tokenize()
method to separate the CSV line into a set of values. This method takes up to four arguments:
We tell String::tokenize()
to separate each line taking into account that any expression enclosed between quotes can include the separator character, in which case it should be ignored. We then use PHP's array_combine()
function so that each line becomes an associative array, indexed by field name and having as its values the corresponding field value.
As the string returned by the String::tokenize()
method includes the boundary strings defined in the leftBound
and rightBound
arguments if they were part of the original string, we proceed to remove them from each line.
We then add a random UUID string as the value for each line's id
field, using the String::uuid()
method. This string will be unique to each line, and should never repeat itself, even across separate requests.
More information about UUIDs can be obtained at http://en.wikipedia.org/wiki/Universally_unique_identifier.
Finally, we go through each line and output a dynamically generated string through the String::insert()
method. This method takes up to three arguments:
str
: String that contains the variable placeholders that should be replaced.data
: Associative array in the form variable => value
, used to replace the variable placeholders with their respective value.options
: Set of options to define how the method should behave. Available options:escape
: Character to use when looking to escape the string used in the before
option. Defaults to
.format
: Regular expression used to find variable placeholders.clean
: If specified, it will clean the replaced string through the String::cleanInsert()
method. Defaults to false
, which means no cleaning is done.In our example, we use the string [:id] Hello :name! Your email is\: :email
. This string contains three variable placeholders: :id, :name
, and :email
. Each of those get replaced by the respective value in the associative array that is passed as the second argument to the String::insert()
method.
3.16.81.33