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
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:
echo 'this is a simple string with a end of line\r\n';
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
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;
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';
strlen($str)
$lastPart = str_replace("/", "", $lastPart);
$matchedTextToRender = preg_replace('/<code[\s]+babel/', '<code javascript', $match);
$localCount = count(preg_split("/<\/p>|<\/h[1-9]{1}>|<br|<\/tr>|<\/li>/",$section['content']));
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.
if (strpos($string, 'prefix') === 0) {
// It starts with 'prefix'
}
$haystack = 'light-gradient';
$suffix = '-gradient';
$suffixStartPosition = strlen($haystack) - strlen($suffix );
if (strrpos($haystack, $suffix ) === $suffixStartPosition){
$mainColorValue = substr($haystack ,0,$suffixStartPosition);
....
}
substr($src, -4) === ".svg"
substr("mystring", -1);
mb_substr("mystring", -1);
ucfirst("name")
The function is strtolower
if ($string === strtolower($string){
echo "string is lowercase"
}
echo str_repeat("-=", 10);
if (strpos($haystack,$needle)!==false){
// foo
} else {
// bar
}
See preg_match
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;
}
var_export($value,true)
Be careful to not use the + sign to concatenate string. Use .
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)
}