Use the string truncation function in php to delete a string from the end.

Source: Internet
Author: User


Fixed a bug in obtaining the controller name.

The controller names end with act and delete act when using it.

1. Original method

// Use the act replacement method to obtain the controller name. Obviously, when the controller name contains three characters of act, it will be replaced with the bug $ actName = str_replace ('AC ', '', 'ad _ client_contactact ');

Controller name ad_client_cont

2. Solution

// Delete three strings from the end

$ ActName = substr ('ad _ client_contactact ', 0,-3 );

Controller name ad_client_contact


1. Truncate from 4th characters to the end of the string, similar to left in asp:

<? Php $ str = "www. Icoa. Cn "; echo substr ($ str, 4) ;?> Output: icoa. Cn

2. The PHP substr function intercepts three characters from the right side, similar to the right in asp:

<? Php $ str = "www. Icoa. Cn "; echo substr ($ str,-3);?> Output :. Cn

3. The PHP substr function intercepts 3 characters from 4th characters:

<? Php $ str = "www. Icoa. Cn "; echo substr ($ str, 4,3) ;?> Output: ico

4. Sometimes we know the start and end of a string, which is an indefinite character in the middle, in this case, we can use substr in addition to the regular expression of the PHP substr function (of course, there are N character methods to be obtained in the middle. Here is just an example of the substr application ):

In this example, the first 4 characters and the last 3 characters are removed, and the intermediate string is output:

<? Php $ str = "www. Icoa. Cn "; echo substr ($ str, 4,-3);?> Output: icoa
5. View Chinese characters
$ A = "straight, vertical, horizontal, square," $ a = substr ($ a, 0,-1); the output result is: straight, vertical, horizontal, and Square

6. Search for the specified string and delete it.

$ A = "abcababa"; $ count = strpos ($ a, "AB"); $ str = substr_replace ($ a, "", $ count, 2); output result: cababa


The code is short, but it is also a small algorithm!


7. Delete any character in the string.

Function delStr ($ start, $ end, $ orgenStr) {// read the first part of the string to be deleted, and assigned to $ temp // the position at which the strpos character is read for the first time. // substr reads the substring at the specified start and end positions. // echo $ before. "-". $ last; $ temp = $ orgenStr; while (strpos ($ temp, $ start) & strpos ($ temp, $ end) {$ temp = substr ($ temp, 0, strpos ($ temp, $ start )). substr ($ temp, strpos ($ temp, $ end) + strlen ($ end); // read the last part of the string to be deleted, and connect the front and back parts, and assign $ temp // return the final string} return $ temp;} // application instance $ a = "aaaa12345678bbbbtttttttttttttttttttttttaaaa12345678bbbb success"; $ B = "1234 "; $ c = "5678"; echo delStr ($ B, $ c, $ );

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.