Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
15
Help us understand the problem. What is going on with this article?

More than 5 years have passed since last update.

@a_yasui

空白を削除する

普通に今まで、'/([\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);
}

15
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
15
Help us understand the problem. What is going on with this article?