PHP5: Strings

Posted on February 23, 2011

Let’s take a look at the new functionality that PHP5 adds for working with strings.

New Functions

The str_split() function splits a string into an array of chunks. This is like explode(), preg_split(), and similar functions, but it splits a string based on size rather than by a pattern. By default each chunk is 1 character of the string, but you can specify a larger chunk size. This might be useful if you need to split a large string into smaller chunks for transmission or storage — compare it to chunk_split() or wordwrap(), which insert a delimiter between chunks rather than creating an array.

$str = "Hello Friend";
$arr = str_split($str, 3);
print_r($arr);

/* displays:
Array
(
[0] => Hel
[1] => lo
[2] => Fri
[3] => end
)
*/

strpbrk() is a search function akin to strstr(). It searches a string for a set of characters and returns the first occurrence of a matching character to the end of the string. It is case sensitive. Could be handy for stripping the beginning of string before an important character:

$text = '@arrowquick: This is a tweet.';
echo trim(strpbrk($text, ' ')); // echoes "This is a tweet."

The substr_compare() function is similar to substr() — it looks for a string inside another string — but it returns 0, 1, or -1 based on where the substring appears based on your guess/expectation. Obviously it’s meant to be used as an equivalency test for sorting, in the same style as strcmp(). Take a look at the examples in the manual to get an idea of how it works.

PHP5 also adds a complement to ucfirst(), named lcfirst(), that makes the first character in a string lowercase. I’m not sure where this is useful, since you usually want to shift the first character to uppercase, or change all characters to lowercase, but it’s available if you need it.

On the other hand, str_getcsv() is a useful addition. It takes a comma-separated row of text (such as from a CSV) and parses it into an array. Previously, the only method to do this was fgetcsv(), which requires a file pointer. (The fgetcsv() function can handle multiple rows at a time, however.)

A new function called preg_filter() is also added to the PCRE extension. It works just like preg_replace(), except that it only returns elements that had positive matches. Take a look at the following code:

$subject = array('1', 'a', '2', 'b', '3', 'A', 'B', '4');
$pattern = array('/d/', '/[a-z]/', '/[1a]/');
$replace = array('A:$0', 'B:$0', 'C:$0');

echo "preg_filter returnsn";
print_r(preg_filter($pattern, $replace, $subject));

echo "preg_replace returnsn";
print_r(preg_replace($pattern, $replace, $subject));

The above code will display:

preg_filter returns
Array
(
[0] => A:C:1
[1] => B:C:a
[2] => A:2
[3] => B:b
[4] => A:3
[7] => A:4
)
preg_replace returns
Array
(
[0] => A:C:1
[1] => B:C:a
[2] => A:2
[3] => B:b
[4] => A:3
[5] => A
[6] => B
[7] => A:4
)

Note that the same elements in the original array are matched and replaced with the new text. However, preg_filter doesn’t return elements 5 and 6 because they didn’t match any of the patterns.

Nowdoc & Heredoc

PHP5 introduces a new method for defining strings. If you remember, PHP4+ has a syntax called Heredoc which lets you easily define a multiple-line string:

$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;

PHP5 adds a variation called Nowdoc. The syntax is almost the same as Heredoc, except you put single quotes around the first use of the identifier:

$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;

The single quotes are a good reminder, because unlike Heredoc, Nowdoc blocks are not parsed for variables or escape sequences. The manual describes it this way: “Nowdocs are to single-quoted strings what heredocs are to double-quoted strings.”

To coincide with this, you can now put double quotes around Heredoc identifiers. So the first example can also be written as:

$str = <<<"EOD"
Example of string
spanning multiple lines
using heredoc syntax.
EOD;

You can also use the Heredoc and Nowdoc syntax in static and class situations:

// Static variables
function foo()
{
  static $bar = <<<LABEL
Nothing in here...
LABEL;
}

// Class properties/constants
class foo
{
  const BAR = <<<FOOBAR
Constant example
FOOBAR;

  public $baz = <<<FOOBAR
Property example
FOOBAR;
}

Binary Strings

Finally, in PHP5 you are able to cast values to “binary strings”. There is very little information about this in the manual… I think this will be implemented more in PHP6, but maybe it is useful for storing the raw binary version of a string without regard to character encoding?

$binary = (binary) $string;
$binary = b"binary string";

Leave a Reply

  1.  

    |