Php processing string

Source: Internet
Author: User
: This article describes how to process strings in php. For more information about PHP tutorials, see. 1. get the string length

$ Str = 'abc'; $ str1 = 'Chinese'; echo strlen ($ str ),'
', Strlen ($ str1); output result: 36

2. search for strings

// Locate the first occurrence of a string $ char = 'a'; $ str = 'abc'; $ bRet = strpos ($ str, $ char ); if ($ bRet = false) {echo "$ str does not contain $ char";} else {echo "$ str contains $ char";} echo'
'; Output result: abc contains

3. replace string

// Replace string str_replace () $ str = 'good morning'; $ str1 = str_replace ('moring', 'afternoon', $ str); echo $ str ,'
', $ Str1; output: Good morningGood morning // replace string strtr () $ str =' get on the bus, go to work, get off, get off class, get off work '; $ str1 = strtr ($ str, ['Up' => 'down', 'down' => 'Up']); echo $ str ,'
', $ Str1; output result: boarding, attending, getting to work, getting off, getting on, going to work

4. truncate a string

// Truncate the string substr (). The parameters are the target string, starting position, and the truncate length $ str = 'East, west, home is best'; echo $ str ,'
', Substr ($ str,); output result: East, west, home is bestEast

5. split the string

// Split string explode () $ str = 'Cook, play, and play sub'; $ arr = explode (',', $ str); echo $ str ,'
'; Print_r ($ arr); output result: cook, play, and recipe Array ([0] => Cook [1] => play [2] => Cook)

6. Merge arrays into strings

$ Arr = ['0' => 'cook sub', '1' => 'player', '2' => 'cook sub']; $ str = implode ($ arr, ';'); print_r ($ arr); echo'
'; Echo $ str; output result: Array ([0] => Cook [1] => play [2] => Cook) cook, play, and Cook

'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
  • '). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script

    The above describes the php processing string, including the content, hope to be helpful to friends who are interested in the PHP Tutorial.

    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.