2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPで特定の一文字を削除する

Last updated at Posted at 2017-12-20

ダメ文字を削除するときに遠回りで作業したことをふと思い出したので戒めのメモ。

例)「㴘㵈㔭䖽㪓䁢㻩㠱䠨㶬㩡㐕䞉㘉䃯䄰䯈䍡䛸䤥」この中の文字が含まれていたら削除する。

遠回りな方法

$str = '䛸置換対象文字列㔭';// 置換対象文字列
$target_character = '㴘㵈㔭䖽㪓䁢㻩㠱䠨㶬㩡㐕䞉㘉䃯䄰䯈䍡䛸䤥';// 置換する文字
$character_list = preg_split('//u', $target_character, null, PREG_SPLIT_NO_EMPTY);// 置換する文字を1文字ずつ配列に分ける
$result = str_replace($character_list, '', $str);
var_dump($result);// string(21) "置換対象文字列"

近道な方法

$str = '䛸置換対象文字列㔭';// 置換対象文字列
$result = mb_ereg_replace('[㴘㵈㔭䖽㪓䁢㻩㠱䠨㶬㩡㐕䞉㘉䃯䄰䯈䍡䛸䤥]', '', $str);
var_dump($result);// string(21) "置換対象文字列"

参考

preg_split
mb_ereg_replace

2
0
0

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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?