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

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

$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文字がまったくないか、連続するかという意味
[|] |だけだとメタ文字になってしまうので[]で文字列化

kusano00
いいサービスをつくる。
e2info
イーツー・インフォはITと人の力でお客様の課題を、ベストな解決策でいい明日へ導くITのプロフェッショナル集団です
https://www.e2info.co.jp/
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
ユーザーは見つかりませんでした