PHP Development Notes Series (ii)-string usage

Source: Internet
Author: User
Tags first string
PHP Development Note Series (ii)-string usage

After the "PHP Development Note Series (a)-pdo use", today opened a PHP development in the processing of strings, "PHP Development Note Series (ii)-string use", the formation of the "PHP Development Note series" in the second chapter.

Strings are required to be processed by any development language, and in PHP strings can be defined using single quotation marks (') or double quotation marks ("). What is the difference between a single quote and a double quote? That is, the variables in the double quotes are replaced by the variable values, and the contents of the single quotation marks are output as-is. The following string processing scenarios will be encountered in the development of daily programs.

1. Accessing strings in array form (strlen)
file:str-lengh.phpurl:http://localhost:88/str/str-lengh.php
 
   ";        For loop access array    //for ($i =0; $i
 
  
"; } //While iterating through the array $i =0; while ($i
"; $i + + }?>

2. Remove all HTML tags from the text (strip_tags)
file:str-strip-tags.phpurl:http://localhost:88/str/str-strip-tags.php
 
   Hello world!

Hello world!

Hello world!

"; Output the original string content echo "Original Text:"; echo $text. "
"; After removing all HTML tags, output echo "Destination Text (after Strip_tags)". "
"; echo strip_tags ($text). "
"; HTML tags in strings are not closed $text = "

Hello world! "; Remove all HTML tags to output echo "Original Text:"; echo $text. "
"; After removing all HTML tags, output echo "Destination Text (after Strip_tags)". "
"; echo strip_tags ($text). "
";? >


Note: If the value of $text is

Hello world!, less.

So

Will not be removed by the strip_tags function, thus affecting the subsequent format output, so that all successive outputs have a H1 heading style.

3. Escaping HTML entities (Rawurlencode)

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


4. Force text wrapping Display (wordwrap)
The wordwrap function can wrap a long text in a line with the specified string.
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 positioning and substitution (Strpos, str_replace)
string positioning uses the Strpos function, which returns the first occurrence of a string in another string, similar to the role of the IndexOf () method of the String class in Java:
file:str-strpos.phpurl:http://localhost:88/str/str-strpos.php
 
    


string substitution uses the Str_replace function, which replaces the text in a partial string, similar to the role of 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)
A string comparison can be used to compare the size between two strings, similar to the Compare method of string in Java, if the return value is >0, the first string is larger than the second, and the second is larger than the first, and if 0 is equal.
file:str-compare.phpurl:http://localhost:88/file/str-compare.php
 
    


7. String interception (SUBSTR)
string truncation can be used to intercept a string of a specified length from a specified position in a substring, which is convenient for substring value extraction.
file:str-sub.phpurl:http://localhost:88/file/str-sub.php
 
     ';    Echo ' Destination String: '. $newStr. '
';? >


8. Count substring occurrences (substr_count)
You can 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. String splitting and assembling (explode, implode)
string splitting splits a string into an array by a specified delimiter, similar to the role of the spilt () method of the string class in Java. Strings are assembled by assembling the data in the array according to a delimiter 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 front and back 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. Formatted output (printf)
formatted output uses the printf function or the sprintf function, similar to the function of the printf function in the C language:
file:str-printf.phpurl:http://localhost:88/str/str-printf.php
 
    


This 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.