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

正規表現を使って特定の文字列から前を削除したい

Posted at

$array = Array(
    0 => 'ユーザー1|一郎',
    1 => 'ユーザー2|次郎',
    2 => 'ユーザー3|三郎',
    3 => 'ユーザー4|四郎',
);

文字列「|」 から前を削除して


$array = Array(
    0 => '一郎',
    1 => '次郎',
    2 => '三郎',
    3 => '四郎',
);

のようにしたい。

##やったこと


$array = preg_replace('/.*[|])/', '', $array);  

####解説


preg_replace関数
preg_replace( $正規表現パターン , $置換後の文字列 , $置換対象の文字列 )

正規表現
// 正規表現パターンの範囲を明示する(デリミタ)。
.* とにかくなんでもいい1文字がまったくないか、連続するかという意味
[|] |だけだとメタ文字になってしまうので[]で文字列化

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