LoginSignup
22
16

More than 5 years have passed since last update.

空白を削除する

Posted at

普通に今まで、'/([\s]|[\xe3\x80\x80])/'とか、こんな感じでいいと思ってたら、そうでもなかった件が出てきたのでメモ書き

<?php

function remove_white ($str){
  return preg_replace('/(?:[\s]|[\xe3\x80\x80])+/', '', $str);
}

半角スペース「 」とタブ「 」とU+3000 の全角スペース「 」だけでいいと思ってたけど、wikipediaの http://ja.wikipedia.org/wiki/スペース を見たらそれだけではない様子。

U-Point UTF-8 (in literal) Name
U+0009 \x09 CHARACTER TABULATION
U+0020 \x20 SPACE
U+00A0 \xc2\xa0 NO-BREAK SPACE
U+2002 \xe2\x80\x82 EN SPACE
U+2003 \xe2\x80\x83 EM SPACE
U+2004 \xe2\x80\x84 THREE-PER-EM SPACE
U+2005 \xe2\x80\x85 FOUR-PER-EM SPACE
U+2006 \xe2\x80\x86 SIX-PER-EM SPACE
U+2007 \xe2\x80\x87 FIGURE SPACE
U+2008 \xe2\x80\x88 PUNCTUATION SPACE
U+2009 \xe2\x80\x89 THIN SPACE
U+200a \xe2\x80\x8a HAIR SPACE
U+200b \xe2\x80\x8b ZERO WIDTH SPACE
U+3000 \xe3\x80\x80 IDEOGRAPHIC SPACE
U+FEFF \xef\xbb\xbf ZERO WIDTH NO-BREAK SPACE

割と種類あるので無理やり対応するとこんな感じ。

<?php

function remove_white ($str){
  return preg_replace(
    '/(?:'
    .'(?:\x09)'
    .'|(?:\x20)'
    .'|(?:\xc2\xa0)'
    .'|(?:\xe2\x80\x82)'
    .'|(?:\xe2\x80\x83)'
    .'|(?:\xe2\x80\x84)'
    .'|(?:\xe2\x80\x85)'
    .'|(?:\xe2\x80\x86)'
    .'|(?:\xe2\x80\x87)'
    .'|(?:\xe2\x80\x88)'
    .'|(?:\xe2\x80\x89)'
    .'|(?:\xe2\x80\x8a)'
    .'|(?:\xe2\x80\x8b)'
    .'|(?:\xe3\x80\x80)'
    .'|(?:\xef\xbb\xbf)'
    .')+/',
    '',
    $str);
}

22
16
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
16