Trim characters


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. function trimchars($chars, $subject)
  2. {
  3. return preg_replace("/\s*([$chars])\s*/", '\1', $subject);
  4. }
  5.  
  6. # V
  7. # a b c d e => a bcd e
  8. trimchars('c', 'a b c d e');
  9.  
  10. # V V
  11. # 23 + 2 = 25 => 23+2=25
  12. trimchars('+=', '23 + 2 = 25');
  13.  
  14. # V V V V V
  15. # body{background:#fff;color:#fff;}
  16. trimchars('{}:;', 'body { background: #fff; color: #fff; }');

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.