PHP Development notes series (II)-string usage

Source: Internet
Author: User
PHP Development notes series (II)-string usage & nbsp; after the PHP Development notes series (I) -PDO usage: Today, we opened the PHP Development notes series (II)-string usage, which is the second article in the PHP Development notes series. & Nbsp; string is a series of PHP Development notes that must be processed by any development language (II)-string usage

After "PHP Development notes series (I)-use of PDO", today I opened a series of PHP Development notes series (II)-use of strings, the second article in the PHP Development notes series.

Strings must be processed by any development language. in PHP, strings can be defined using single quotation marks (') or double quotation marks. What is the difference between single quotes and double quotes? That is, the variable in double quotation marks will be replaced by the variable value, and the content in single quotation marks will be output as is. Below we will sort out the string processing scenarios that will be encountered during routine program development.

1. access the string (strlen) as an array)
File: str-lengh.phpurl: http: // localhost: 88/str/str-lengh.php
 "; // For loop access array // for ($ I = 0; $ I
 "; //} // While loop access array $ I = 0; while ($ I
 "; $ I ++}?>

2. remove all HTML tags in the text (strip_tags)
File: str-strip-tags.phpurl: http: // localhost: 88/str/str-strip-tags.php
 "; // Output echo After removing all html tags" Destination Text (After strip_tags )"."
"; Echo strip_tags ($ text )."
"; // The html tag in the string is not closed $ text =" hello world! "; // After removing all html tags, output echo" Original Text: "; echo $ text ."
"; // Output echo After removing all html tags" Destination Text (After strip_tags )"."
"; Echo strip_tags ($ text )."
";?>

Note: If the value of $ text is hello world !, If it is missing, it will not be removed by the strip_tags function, thus affecting the subsequent format output, so that all subsequent output will have the style of the h1 title.

3. escape html entities (rawurlencode)
file:str-entities.phpurl:http://localhost:88/str/str-entities.php
 ";        echo rawurlencode($text)."
";?>


4. force text line display (wordwrap)
The wordwrap function can fold long text according to the specified string length.
file:str-wordwrap.phpurl:http://localhost:88/str/str-wordwrap.php
 ";    echo $text."
"; echo $text." "; echo "Destination text(after wrap):"."
"; echo wordwrap($text, 50, "
")."
";?>


5. string location and replacement (strpos, str_replace)
The strpos function is used to locate a String. This function returns the first position where a String appears in another String. it is similar to the function of the indexOf () method of the String class in JAVA:
file:str-strpos.phpurl:http://localhost:88/str/str-strpos.php
 


String replacement uses the str_replace function, which replaces the text in some strings. This function is similar to the replace () method of the String class in JAVA:
file:str-strreplace.phpurl:http://localhost:88/str/str-strreplace.php
 ";    echo $text."
"; echo " "; echo "Destination text(replace):"."
"; echo str_replace(" ", , $text)."
"; ?>


6. string comparison (substr_compare)
String comparison can be used to compare the size of two strings, similar to the compare method of String in JAVA. if the return value is greater than 0, the first String is larger than the second String, if it is 0, it indicates equal.
file:str-compare.phpurl:http://localhost:88/file/str-compare.php
 


7. string truncation (substr)
String truncation can be used to extract strings of the specified length from the specified position of a string. it is convenient to extract string values.
file:str-sub.phpurl:http://localhost:88/file/str-sub.php
 ';    echo 'Destination String: '.$newStr.'
';?>


8. count the occurrence times of the substring (substr_count)
Use the substr_count function to count the number of times a substring appears in the parent string.
file:str-count.phpurl:http://localhost:88/file/str-count.php
 


9. split and assemble strings (explode, implode)
String splitting can split a String into arrays according to a specified separator, which is similar to the spilt () method of the String class in JAVA. During string assembly, the data in the array is assembled based on a separator to form a new string.
file:str-explode-implode.phpurl:http://localhost:88/str/str-explode-implode.php
 ";    echo $text."
"; echo " "; $sentenses = explode(". ", $text); echo "Destination text(explode):"."
"; foreach ($sentenses as $sentense){ echo $sentense."
"; } echo " "; $newText= implode($sentenses, ". "); echo "Destination text(implode):"."
"; echo $newText."
"; ?>


10. remove the leading and trailing spaces (trim) of the string)
file:str-trim.phpurl:http://localhost:88/str/str-trim.php
 ";    echo strlen($text)."
"; echo " "; echo "Destination text(trim):"."
"; echo strlen(trim($text))."
"; ?>


11. format the output (printf)
The formatting output uses the printf function or sprintf function, which is similar to the printf function in C language:
file:str-printf.phpurl:http://localhost:88/str/str-printf.php
 


Address: http://ryan-d.iteye.com/blog/1543225

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.