Summary of common string operations in php study notes

  • 2021-12-13 07:40:21
  • OfStack

This article illustrates the common operations of php strings. Share it for your reference, as follows:

Definition of string

You can define strings with single or double quotation marks


<?php
$str = "hello";
$str = 'hello';
>?

heredoc and nowdoc define large strings

Difference: It is the same as the difference between single quotation marks and double quotation marks defining strings


<?php
//heredoc Use of 
$str3 = <<<MARK
hello
 world!
  ok;$a
   puzhong!
MARK;
echo $str3."<br/>";
//nowdoc Use of 
$str4 = <<<'MARK'
hello
 world!
  ok;$a
   puzhong!
MARK;
echo $str4."<br/>";
?>

The difference between single quotation marks and double quotation marks

1) Escape difference: Single quotation mark escape situation:\\,\ ', all others are output as they are
Double quote escape:\,,\ ",\ n,\ r,\ t, and so on

2) Speed difference: Variables cannot be loaded in single quotation marks, while variables can be loaded in double quotation marks
In speed, single quotation marks are faster than double quotation marks

Common functions of strings

1) Calculate the length of a string
strlen (): Calculate the length according to the number of bytes occupied by the string
mb_strlen (): Calculates string length by encoding


<?php
$str = "hello";
$str2 = " China 123";
echo strlen($str)."<br/>"; // Output 5
echo strlen($str2)."<br/>";// Output 9 , utf8 Character encoding 1 Chinese characters account for 3 Bytes 
echo mb_strlen($str2,"utf-8")."<br/>";// Output 5 , according to u8 Encoding, calculating the length of characters 
?>

2) Calculate the position function of the substring

strpos (): Case sensitive to find the first occurrence of a substring
stripos (): Find the first occurrence of a substring without partitioning case
strrpos (): Case sensitive to find the last occurrence of a substring
strripos (): Find the last occurrence of a substring case-insensitive


<?php
$str = "aAbBcdefgAa";
$char = "A";
echo strpos($str, $char)."<br/>";// Output 1 Case sensitive 
echo stripos($str,$char)."<br/>";// Output 0 , case-insensitive 
echo strrpos($str,$char)."<br/>";// Output 9 Case sensitive 
echo strripos($str, $char)."<br/>"; Output 10 , // Case insensitive 
?>

3) Replace string functions

str_replace (): Content in case-sensitive replacement string
str_ireplace (): Does not partition the contents of the case replacement string
strtr (): Replace in batches (you can use some correspondence of arrays)
substr_replace (): Replaces the contents of the string according to the position of the string and the specified length


<?php
$str = "hi,Fuck";
echo str_replace("fuck","f**k",$str)."<br/>";// Output :hi,Fuck Is case sensitive, so nothing was found to replace 
echo str_ireplace("fuck", "f**k", $str)."<br/>";// Output :hi,f**k , case-insensitive 
$str = " Men, women, boys, girls ";
echo strtr($str, array(" Male "=>" Female "," Female "=>" Male "));// Output: Women , Man , Girl , Boy 
$str = "hello,webbc,welcome";
echo substr_replace($str,"webxx",6,5);// Output: hello,webxx,welcome
?>

4) Intercept substring function

substr (): Truncates a string according to the starting position and the specified length


<?php
$str = "hello,webbc,welcome!";
echo substr($str,6,5)."<br/>";// Output: webbc
echo substr($str,6,-1)."<br/>";// Output: webbc,welcome A negative number indicates that the end position is counted from back to front 
echo substr($str,-8,-1);// Output: welcome A negative number indicates that the starting position is counted from back to front 
?>

5) Merge/Split Strings

explode (): Split a string into an array according to a certain character
implode (): Merge arrays into strings according to a certain character


<?php
$str = " Warrior , Spurs , Thunder , Clippers , Pioneer , Calf , Grizzly bear , Rocket ";
$arr = explode(",",$str);
var_dump($arr);
/*
 Output: array
  0 => string ' Warrior ' (length=6)
  1 => string ' Spurs ' (length=6)
  2 => string ' Thunder ' (length=6)
  3 => string ' Clippers ' (length=6)
  4 => string ' Pioneer ' (length=9)
  5 => string ' Calf ' (length=6)
  6 => string ' Grizzly bear ' (length=6)
  7 => string ' Rocket ' (length=6)
*/
echo implode($arr,"-");// Output: Warrior - Spurs - Thunder - Clippers - Pioneer - Calf - Grizzly bear - Rocket 
?>

6) Handling spaces

ltrim (): Delete the left leading space
rtrim (): Delete right leading space
trim (): Delete left and right lead spaces


<?php
$str = "  hello world  ";
echo ltrim($str)."a"."<br/>";// Output :hello world a
echo "a".rtrim($str)."<br/>";// Output :a hello world
echo trim($str)."<br/>";// Output :hello world
?>

7) String escape function

addslashes (): Escape special characters
stripslashes (): Restore escaped characters


<?php
$str = "Is your ' name O'reilly?";
$a = addslashes($str);
echo $a;// Output: Is your \' name O\'reilly?
echo stripslashes($a);// Output :Is your ' name O'reilly?

8) HTML and string conversion

htmlspecialchars (): Converts a string to an HTML entity
htmlspecialchars_decode (): Converts an HTML entity to a string


<?php
$str = "<b> I'm bold &nbsp;&nbsp; Font </b>";
echo $str; // Output: I am bold   Font 
$htmlstr = htmlspecialchars($str);
echo $htmlstr;// Output: <b> I'm bold &nbsp;&nbsp; Font </b>
echo htmlspecialchars_decode($htmlstr);// Output: I am bold   Font 
?>

9) Retrieve a string

strstr (): Find the first occurrence of a string
substr_count (): Counts the number of string occurrences


<?php
//heredoc Use of 
$str3 = <<<MARK
hello
 world!
  ok;$a
   puzhong!
MARK;
echo $str3."<br/>";
//nowdoc Use of 
$str4 = <<<'MARK'
hello
 world!
  ok;$a
   puzhong!
MARK;
echo $str4."<br/>";
?>

0

10) Conversion functions for strings

strtolower (): All converted to lowercase
strtoupper (): All converted to uppercase
ucfirst (): Converts the first letter of a string to uppercase
ucwords (): Capitalize the first letter of each word in a string


<?php
//heredoc Use of 
$str3 = <<<MARK
hello
 world!
  ok;$a
   puzhong!
MARK;
echo $str3."<br/>";
//nowdoc Use of 
$str4 = <<<'MARK'
hello
 world!
  ok;$a
   puzhong!
MARK;
echo $str4."<br/>";
?>

1

For more readers interested in PHP related contents, please check the topics of this site: "Summary of Common Functions and Skills of php", "Summary of Usage of php String (string)", "Encyclopedia of Operation Skills of PHP Array (Array)", "Introduction to Basic Grammar of PHP", "Introduction to Database Operation of php+mysql" and "Summary of Operation Skills of Common Database of php"

I hope this paper is helpful to everyone's PHP programming.


Related articles: