Return to Snippet

Revision: 30310
at August 11, 2010 17:31 by Sverri


Updated Code
function trimchars($chars, $subject)
{
  return preg_replace("/\s*([$chars])\s*/", '\1', $subject);
}

#                 V
# a b c d e => a bcd e
trimchars('c', 'a b c d e');

#                  V V
# 23 + 2 = 25 => 23+2=25
trimchars('+=', '23 + 2 = 25');

#     V          V    V     V    V
# body{background:#fff;color:#fff;}
trimchars('{}:;', 'body { background: #fff; color: #fff; }');

Revision: 30309
at August 11, 2010 17:27 by Sverri


Initial Code
function trimchars($chars, $subject)
{
  return preg_replace("/\s*([$chars])\s*/", '\1', $subject);
}

#                 V
# a b c d e => a bcd e
trimchars('c', 'a b c d e');

#                  V V
# 23 + 2 = 25 => 23+2=25
trimchars('+=', '23 + 2 = 25');

#     V          V    V     V    V
# body{background:#fff;color:#fff;}
trimchars('{}:;', 'body { background: #fff; color:#fff; }');

Initial URL


Initial Description
Looks for $chars in $subject and removes any preceding and succeeding whitespace from the provided characters. It works much like trim(), except it trims characters instead of the string itself.

Initial Title
Trim characters

Initial Tags


Initial Language
PHP