About
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
Initialization
Quotes
Single Quoted
- enclose it in single quotes (the character ').
echo 'this is a simple string';
Double-quoted
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';
Doc
Heredoc
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
Nowdoc
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.
EOD;
Function
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';
Length
strlen($str)
All String functions
Replace
- Exact: Suppressing a / str_replace
$lastPart = str_replace("/", "", $lastPart);
- Regexp: preg_replace. Example: <code babel by <code javascript
$matchedTextToRender = preg_replace('/<code[\s]+babel/', '<code javascript', $match);
Split
- 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)
Contains
- str_contains (from php 8)
if (str_contains('abc', '')) {
echo "Checking the existence of the empty string will always return true";
}
- strpos for an exact match
$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)
Extract
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
- php8: str_ends_with
- with strrpos
$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
ucfirst("name")
First and last position
Lowercase
The function is strtolower
- isLowercase test snippet.
if ($string === strtolower($string){
echo "string is lowercase"
}
Uppercase
Repeat
echo str_repeat("-=", 10);
Contains
if (strpos($haystack,$needle)!==false){
// foo
} else {
// bar
}
- From php 8.0, str_contains
Match
See preg_match
Flow
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
- var_export - output a php string.
var_export($value,true)
Support
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
-
- Concat: .