Summary of some strings in php and summary of php Strings

Source: Internet
Author: User

Summary of some strings in php and summary of php Strings

Php comes with a function to intercept strings. It can only process English characters, and numbers cannot intercept Chinese characters. If you need it, you can refer to it. It is easier to use later, the first is intended for beginners.

Php // construct the string $ str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; echo "original string :". $ str. ""; // intercept in various ways $ str1 = substr ($ str, 5); echo "starts from 5th characters to the end :". $ str1. "; $ str2 = substr ($ str, 9, 4); echo" starts from 9th characters and starts with 4 characters :". $ str2. "; $ str3 = substr ($ str,-5); echo" returns the last five characters :". $ str3. ""; $ str4 = substr ($ str,-8th); echo "returns four characters from the last characters :". $ str4. ""; $ str5 = substr ($ str,-8,-2); echo "starts from the last 8th characters to the last 2nd characters :". $ str5. "";?>
Supports intercept between Chinese and English.
/* ---------------------------------------------------- Parameter: $ str_cut string to be truncated $ length the maximum length allowed for string display function: intercept width and halfwidth (Chinese and English) mixed strings to avoid garbled characters. */function substr_cut ($ str_cut, $ length) {if (strlen ($ str_cut)> $ length) {for ($ I = 0; $ I <$ length; $ I ++) if (ord ($ str_cut [$ I])> 128) $ I ++; $ str_cut = substr ($ str_cut, 0, $ I ). ".. ";}return $ str_cut ;}?>
1. // string inversion Function$ Str = 'hello'; // ----> ollehecho strrev ($ str ). '<br/>'; // This is a function provided by the system. // write a function to reverse it. $ strArr = str_split ($ str ); // Array ([0] => h [1] => e [2] => l [3] => l [4] => o) $ str1 = ''; for ($ I = count ($ strArr)-1; $ I >=0; $ I --) {$ str1. = $ strArr [$ I];} echo $ str1;2. // get the file suffix. jpg or jpg.Using file='abc.exe.jpg '; echo strrchr ($ file ,'. '). '<br/>'; // the system function used. Find the file name echo strrev (strstr (strrev ($ file ),'. ', true )). '<br/>'; // strstr () returns the first occurrence of a string. If true is added, the returned result is. previous string echo substr ($ file, strrpos ($ file ,'. ')). '<br/>'; // strrpos () returns the last time. and then intercept $ strArr = explode ('. ', $ file); echo $ strArr [count ($ strArr)-1]. '<br/>'; // The explode () function splits the string into an array by point. // echo array_pop ($ strArr ). '<br/>'; // echo end ($ strArr ). '<br/>'; echo pathinfo ($ file, PATHINFO_EXTENSION ). '<br/>'; // The information returned by the pathinfo () function is the file path echo strrev (substr (strrev ($ file), 0, strpos ($ file ,'. '))). '<br/> ';3. // set 1234567890 -----> 1,234,567,890$ Str = '000000'; $ strArr = str_split (strrev ($ str), 3); echo strrev (implode (',', $ strArr )). '<br/>'; // ----> 1,234,567,890

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.