PHP - Strings

  »   PHP  »   PHP Tutorial - Strings

A string in PHP is a simple character concatenation like "Hello world!", "Lorem Ipsum..." etc. We already learned about how to set a string in PHP using double and single quotes, nowdoc and heredoc syntax.

Strings are one of the most used types of data. The simplest way to assign string values is to wrap it into single or double quotes like follows:

PHP<?php
	$a = 'Simple string value';	 
	$b = "Another string value using double quotes.";

	echo $a;
	echo "<br />";
	echo $b;
?>

Escaping quotes in strings

Simple or double quotes are many times found in blocks of text. You can escape quotation marks using the backslash (\) symbol.

PHP<?php
	$a = 'This is how I\'ll be escaping a single quotation mark in PHP.';
	echo $a;
?>

Nowdoc and heredoc syntax

There are also other ways to define string values. Besides single and double quotation you can use heredoc and nowdoc syntax if you run PHP 5.3.0 or later. Here is a heredoc example:

PHP<?php
$here = <<<EOT
    I'm here to show you how to use heredoc syntax!
EOT;

	echo $here
?>

Nowdoc syntax similar to heredoc, but just like single quotes, it doesn't allow using and evaluating variable inside it. Here is a side by side example so you can compare it:

PHP<?php
$foo = 'bar';

// heredoc example
$here = <<<EOT
    I'm here, $foo!
EOT;

// Nowdoc example
$now = <<<'EOT'
    I'm now, $foo!      
EOT;

echo $here // prints I'm here, bar!
echo $now  // prints I'm now, $foo!
?>

Limits on string type data

According to the PHP manual: (http://php.net/manual/en/language.types.string.php) "as of PHP 7.0.0, there are no particular restrictions regarding the length of a string on 64-bit builds. On 32-bit builds and in earlier versions, a string can be as large as up to 2GB (2147483647 bytes maximum)".

String manipulation using PHP built-in functions

PHP has lots of built-in function that helps you when it comes to working with strings. You can:

  • count characters or words and reverse character order of a string
  • search, search and replace inside the string, split it into multiple strings by a certain character, etc
  • transform string to uppercase, lowercase, capitalize remove leading and trailing white spaces and so on
  • obtain a hash from a given string, convert html tags to entities so you can print the symbols instead of interpret them.

This are just a few of the PHP function, You can check the full reference list of them in the PHP string functions reference page.

PHP count characters, words and reverse order

This is how we count characters and words in PHP. We will use the strlen() and str_word_count() respectively.

PHP<?php
	$string = 'Working with string in PHP is awesome!';

	echo "This string has ".strlen($string). " characters and ". str_word_count($string). " words.";
	echo "<br />Now try to spell it in reverse order :-)<br />";
	echo strrev($string);
?>

PHP search text occurrence in string

strpos() PHP function searches for a given text inside a string. It will return the first character position of the first match and FALSE if it didn't find it.

PHP<?php
	echo strpos('Working with string in PHP is awesome!', "PHP"); // outputs 23
?>

Here is a really useful example from the real-world coding. You will have to use the identical operator (===) to obtain the expected result. You will learn about it in the next tutorial

PHP<?php
	$string = 'The quick brown fox jumps over the lazy dog.';
	$search   = 'lazy';
	$pos = strpos($string, $search);

	if ($pos === false) {
	    echo "We didn't found '$search' inside '$string'";
	} else {
	    echo "We located '$search' inside '$string' at position $pos";
	}
?>

PHP search and replace

Use the str_replace() to perform the search and replace job.

PHP<?php
	$string = 'The quick brown fox jumps over the lazy dog.';
	echo str_replace("brown", "pink", $string); 
?>

str_replace() has a forth param that counts the number of replacements that has been done: str_replace(search, replace, $string, $count)

PHP split string using explode function

The PHP explode() function will split the string into an array every time the separator character it is found. The result will be an indexed array:

PHP<?php
	$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
	$pieces = explode(" ", $pizza);

	echo "<pre>";
	print_r($pieces);
	echo "</pre>";
?>