Posted By

Sverri on 08/11/10


Tagged

trim characters


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

wireplay
manavecplan


Trim characters


 / Published in: PHP
 

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.

  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  

You need to login to post a comment.