Help us understand the problem. What is going on with this article?

空白を削除する

More than 3 years have passed since last update.

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

mohikanz
エンジニアのための雑談コミュニティ
https://mohikanz.slack.com
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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした