Clear all space programs in the article in php-PHP source code

Source: Internet
Author: User
We can use the ereg_replace function to clear the blank or spaces in the string. The following is a page for you to help. We can use the ereg_replace function to clear the blank or spaces in the string. The following is a page for you to help.

Script ec (2); script

Remove all spaces

The Code is as follows:
Function delete ($ str ){
$ Str = trim ($ str );
$ Str = ereg_replace ("\ t", "", $ str );
$ Str = ereg_replace ("\ r \ n", "", $ str );
$ Str = ereg_replace ("\ r", "", $ str );
$ Str = ereg_replace ("\ n", "", $ str );
$ Str = ereg_replace ("", "", $ str );
Return trim ($ str );
}

Remove all comments. Use the preg_replace () function to replace the comments with null (empty, not spaces ).
Remove the space str_replace ('','', $ cat_name) from the string)

The Code is as follows:

$ Str = "This line contains \ tliberal \ r \ n use of whitespace. \ n ";
$ Str = trim ($ str); // first remove the leading and trailing Spaces
$ Str = preg_replace ('/\ s (? = \ S)/', '', $ str); // remove the two or more spaces
$ Str = preg_replace ('/[\ n \ r \ t]/', ', $ str); // replace a non-space with a space.

The preceding example can remove all unnecessary spaces.
First, TRim () is used to remove leading and trailing spaces,
Use preg_replace () to remove duplicate spaces.
(? =) Indicates that only the following spaces are matched with the leading spaces.

The Code is as follows:

$ Str1 = "tt Happy Chinese Valentine's Day! Nr "; // define a character variable, including" space "," t ", horizontal tab," n ", Line Break
// The trim () function is called to remove spaces. The trim () function is used to remove the "" space "," t "Horizontal tab" n "linefeed, and" r "carriage return.
// "\ 0" string Terminator and "xOB" vertical tab. If you want to use this function to filter out special characters, you can specify the second parameter.
Echo trim ($ str1 )."
";
// Remove the tt characters in the $ str1 variable.
Echo trim ($ str1, "tt ")."
";
// Define the variable $ str2, which includes "." And space.
$ Str2 = "... Happy Valentine's Day !... China ...";
// Call the trim () function to remove spaces in the $ str2 variable
Echo trim ($ str2 )."
";
// The ltrim () function is used to remove spaces or specified strings on the left of the string. The default character is the same as trim. Because the second parameter is specified here,
// Therefore, only the "." on the left of the $ str2 variable is removed "."
Echo ltrim ($ str2 ,".")."
";
// The ltrim () function is used to remove spaces on the right side of a string or specify a string. The default character is the same as trim. Because the second parameter is specified here,
// Therefore, only the "." on the left of the $ str2 variable is removed "."
Echo rtrim ($ str2 ,".")."
";
?>

Echo substr ("today is father day! ", 0 )."
";
Echo substr ("today is father day! ", 6, 2 )."
"; // Here, only the 6th characters in the string are captured and only 2 characters are captured.
Echo substr ("today is father day! ",-5, 5 )."
"; // It starts from the last 5th characters of the string and intercepts 5 characters.
Echo substr ("today is father day! ", 0,-5 )."
"; // Here, only the first character in the string is intercepted and the last 5th characters in the string are intercepted.
Echo substr ("today is father day! ",-5,-1 )."
"; // Here, the string starts from the last 5th characters, and the first to the last character is truncated.
?>

Add: Comment and remove spaces in php.

The Code is as follows:


/**
* Remove blank and comments from the code
* @ Param string $ content Code content
* @ Return string
*/
Function strip_whitespace ($ content ){
$ StripStr = '';
// Analyze the php source code
$ Tokens = token_get_all ($ content );
$ Last_space = false;
For ($ I = 0, $ j = count ($ tokens); $ I <$ j; $ I ++ ){
If (is_string ($ tokens [$ I]) {
$ Last_space = false;
$ StripStr. = $ tokens [$ I];
} Else {
Switch ($ tokens [$ I] [0]) {
// Filter various PHP comments
Case T_COMMENT:
Case T_DOC_COMMENT:
Break;
// Filter Spaces
Case T_WHITESPACE:
If (! $ Last_space ){
$ StripStr. = '';
$ Last_space = true;
}
Break;
Case T_START_HEREDOC:
$ StripStr. = "< Break;
Case T_END_HEREDOC:
$ StripStr. = "THINK; \ n ";
For ($ k = $ I + 1; $ k <$ j; $ k ++ ){
If (is_string ($ tokens [$ k]) & $ tokens [$ k] = ';'){
$ I = $ k;
Break;
} Else if ($ tokens [$ k] [0] = T_CLOSE_TAG ){
Break;
}
}
Break;
Default:
$ Last_space = false;
$ StripStr. = $ tokens [$ I] [1];
}
}
}
Return $ stripStr;
}

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.