php - Trim a full string instead of using trim() with characters -
While using a specific delimiter in the data string, I often want to trim it to the final example before exploding.
I have always thought of this:
PHP trim ()
will skip the white space or some characters but if I want to trim the whole string:
$ data = 'cookieDELIMITERchocolateDELIMITER'; $ Data = trim ($ data, 'DELIMITER'); The above will not actually work, because this will not trim the string "DELIMITER", rather instead "D, E, L, I, M, T, R". What I need is:
$ data = 'cookieDELIMITERchocolate';
I appreciate your suggestions!
$ data = 'cookie DELIMITER chocolate DELIMITER'; $ Data = preg_replace ('/ ^ (DELIMITER) * | (DELIMITER) * $ /', '', $ data); Var_dump ($ data); // string (24) "cookie DELIMITER chocolate"
The regular expression 0 + ( *
) matches the beginning of "DELIMITER" ( ^
) or string ( |
) of the string ( $
) will take these matches and replace them with empty strings.
Full Regx explanation
^ matches the beginning of #DELIMITERDELIMITER) # match "DELIMITER" 0 times, Thanks * | # Or (DELIMITER) * matches the end of the "bar" string,
, this means that in the beginning several DELIMITER end of the string Since Preg_replace ()
replaces a global match /, it will receive all events DELIMITER as long as they are connected to the beginning or end of the string.
dot ( ^ (DELIMITER). * | (DELIMITER) * $
) will match the beginning of the string, after that DELIMITER, after any character of 0 + (< Code>. All matches) will be effective if this DELIMITER
Comments
Post a Comment