Common php functions
Strstr (string, string) // truncate from the place where a string appears for the first time to the end
Strrchr (string, string) // captures a string from its last position to its end.
Strpos (string, string [, int]) // position where a string appears for the first time
Strrpos (string, string) // position of the last occurrence of a string
Substr (string, int [, int]) // truncates a string from a specified position. You can specify the length of the string.
Strlen (string) // Obtain the string length. PHP intercepts the characters between the start and end tags.
Method 1: Use explode to split strings
The code is as follows: |
Copy code |
/** * Get the content between $ start and $ end * * @ Param string $ original content character * @ Param string $ start character * @ Param string $ end character * @ Return string */ Function GetStringBetween ($ content, $ start, $ end ){ $ R = explode ($ start, $ content ); If (isset ($ r [1]) { $ R = explode ($ end, $ r [1]); Return $ r [0]; } Return ''; } |
Method 2: Use substr to intercept characters. Because of the php component functions, the string processing efficiency is higher than that of The explode method.
The code is as follows: |
Copy code |
/** * Get the content between $ start and $ end * * @ Param string $ original content character * @ Param string $ start character * @ Param string $ end character * @ Return string */ Function get_string_between ($ string, $ start, $ end ){ $ String = "". $ string; $ Ini = strpos ($ string, $ start ); If ($ ini = 0) return ""; $ Ini + = strlen ($ start ); $ Len = strpos ($ string, $ end, $ ini)-$ ini; Return substr ($ string, $ ini, $ len ); }
|
PHP intercepts all strings that match the start and end tags
The code is as follows: |
Copy code |
/** * Get all every strings between two tags * * @ Param string $ string original string * @ Param string $ start string * @ Param string $ end string * @ Return array */ Function get_all_strings_between ($ string, $ start, $ end) { // Returns an array of all values which are between two tags in a set of data $ Strings = array (); $ StartPos = 0; $ I = 0; // Echo strlen ($ string). "n "; While ($ startPos <strlen ($ string) & $ matched = get_string_between (substr ($ string, $ startPos), $ start, $ end )) { If ($ matched = null | $ matched [1] = null | $ matched [1] = '') break; $ StartPos = $ matched [0] + $ startPos + 1; Array_push ($ strings, $ matched [1]); $ I ++; } Return $ strings; }
Function get_string_between ($ string, $ start, $ end ){ $ Ini = strpos ($ string, $ start ); If ($ ini = 0) return null; $ Ini + = strlen ($ start ); $ Len = strpos ($ string, $ end, $ ini)-$ ini; Return array ($ ini + $ len, substr ($ string, $ ini, $ len )); } |
The characters between the start and end mark of the php truncation, with the number of interceptions and whether repeated options are allowed
The code is as follows: |
Copy code |
/** * Get all every strings between two tags * * @ Param string $ string original string * @ Param string $ start string * @ Param string $ end string * @ Return array */ Function get_all_strings_between ($ string, $ start, $ end) { // Returns an array of all values which are between two tags in a set of data $ Strings = array (); $ StartPos = 0; $ I = 0; // Echo strlen ($ string). "n "; While ($ startPos <strlen ($ string) & $ matched = get_string_between (substr ($ string, $ startPos), $ start, $ end )) { If ($ matched = null | $ matched [1] = null | $ matched [1] = '') break; $ StartPos = $ matched [0] + $ startPos + 1; Array_push ($ strings, $ matched [1]); $ I ++; } Return $ strings; } Function get_string_between ($ string, $ start, $ end ){ $ Ini = strpos ($ string, $ start ); If ($ ini = 0) return null; $ Ini + = strlen ($ start ); $ Len = strpos ($ string, $ end, $ ini)-$ ini; Return array ($ ini + $ len, substr ($ string, $ ini, $ len )); } |
Truncates GB2312 Chinese strings.
The code is as follows: |
Copy code |
<? Php // Truncate a Chinese string Function mysubstr ($ str, $ start, $ len ){ $ Tmpstr = ""; $ Strlen = $ start + $ len; For ($ I = 0; $ I <$ strlen; $ I ++ ){ If (ord (substr ($ str, $ I, 1)> 0xa0 ){ $ Tmpstr. = substr ($ str, $ I, 2 ); $ I ++; } Else $ Tmpstr. = substr ($ str, $ I, 1 ); } Return $ tmpstr; } ?> |
Truncates UTF-8 encoded multi-byte strings.
The code is as follows: |
Copy code |
<? Php // Truncate the utf8 string Function utf8Substr ($ str, $ from, $ len) { Return preg_replace ('# ^ (? : [\ X00-\ x7F] | [\ xC0-\ xFF] [\ x80-\ xBF] +) {0, '. $ from .'}'. '((? : [\ X00-\ x7F] | [\ xC0-\ xFF] [\ x80-\ xBF] +) {0 ,'. $ len. '}). * # s ', '$ 1', $ str ); } ?> |