PHP - String

Card Puncher Data Processing


The string in PHP is implemented as an array of bytes with the ascii character set and an integer indicating the length of the buffer. It has no information about how those bytes translate to characters, leaving that task to the programmer.

If you want to manipulate a string with another character set, you need to use the php multi-bytes function



Single Quoted

  • enclose it in single quotes (the character ').
echo 'this is a simple string';


If the string is enclosed in double-quotes (“), PHP will interpret more escape sequences for special characters such as:

  • Carriage return,
  • Line feed
echo 'this is a simple string with a end of line\r\n';


Language - Here Document


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


Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc.

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


Character Indexing

A string can be seen as an array of characters and can be accessed and modified by specifying the zero-based offset

// Get the first character of a string
$str = 'This is a test.';
$first = $str[0];

// Get the third character of a string
$third = $str[2];

// Modify the third character of a string
$str[3] = 'e';



All String functions

String functions


$lastPart = str_replace("/", "", $lastPart);
$matchedTextToRender = preg_replace('/<code[\s]+babel/', '<code javascript', $match);


  • preg_split: Count the number of line an HTML document by splitting by the p, h, br,tr,li element
$localCount = count(preg_split("/<\/p>|<\/h[1-9]{1}>|<br|<\/tr>|<\/li>/",$section['content']));
  • split (deprecated in favor of preg_split)


if (str_contains('abc', '')) {
    echo "Checking the existence of the empty string will always return true";
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
# contains date, created or modified
preg_match('/date|created|modified/i', $value)


preg_match captures groups and returns them into the $matches third parameter.

Start with

if (strpos($string, 'prefix') === 0) {
   // It starts with 'prefix'

End with

$haystack = 'light-gradient';
$suffix = '-gradient';
$suffixStartPosition = strlen($haystack) - strlen($suffix );
if (strrpos($haystack, $suffix ) === $suffixStartPosition){
    $mainColorValue = substr($haystack ,0,$suffixStartPosition);
  • or last characters ie substr(“mystring”, -1)
substr($src, -4) === ".svg"

Last character

substr("mystring", -1);
mb_substr("mystring", -1);

Upper First Character


First and last position

  • strpos - first position of a character
  • strrpos - last position of a character


The function is strtolower

  • isLowercase test snippet.
if ($string === strtolower($string){
   echo "string is lowercase"





echo str_repeat("-=", 10);


if (strpos($haystack,$needle)!==false){
   // foo
} else {
  // bar


See preg_match


Loop through characters

for ($i = 0; $i < strlen($s); $i++) {
    $char = $s[$i]; 
    echo $char;
for ($i = 0; $i < mb_strlen($s); $i++) {
    $char = mb_substr($s, $i, 1);
   echo $char;

To String



A non-numeric value encountered

Be careful to not use the + sign to concatenate string. Use .

Offset not contained in string

You may encounter this message:

PHP Warning:  strpos(): Offset not contained in string

It means that the offset used in the strpos function is higher than the length of the string minus 1 (offset starts at 0)

Quick fix with a conditional

if ($offset < (strlen($string) - 1)) {
  $pos = strpos($string, $searchChar, $offset)

Documentation / Reference

Discover More
Card Puncher Data Processing
Character set

Character Set in PHP
Card Puncher Data Processing
Datacadamia - Data all the things

Computer science from a data perspective
Card Puncher Data Processing
Php - Boolean

This page is the boolean data type in Php. Because of this dynamic nature, php has a weak typing. It considers then a lot of different value as true or false and it can get really confusing. This article...
Card Puncher Data Processing
Php - URL

in php in php You parse it in two steps: first the string format of the query part with parse_url then you put the key/argument of the query in an array with parse_str. URL encoded then...

Share this page:
Follow us:
Task Runner