LoginSignup
1
0

More than 3 years have passed since last update.

【サルが書く】特定文字列と特定文字列の間の特定文字列を取り除きたいときの操作

Last updated at Posted at 2020-12-23

わきゃ!!

GIGアドベントカレンダー16日目になります!
GIGメンバーの投稿はこちら👇 から見てみてね!
https://qiita.com/organizations/gig-inc

文字列から特定の文字列を取り出す操作は探せばあるけど、特定文字列と特定文字列の間の特定文字列を取り除きたいが見当たらなかったのでこれが役に立てば嬉しいです!

以下の例の場合は<blockquote></blockquote>の間の<p></p>を取り除きたいという例です。
適宜入れ替えてご使用ください。

        $body = '<blockquote>hoge<p>fuga</p>piyo<\/blockquote>';
        $htmlTags = ['<p>', '</p>'];

        $body = preg_replace_callback(
            '/<blockquote>(.*?)<\/blockquote>/',
            function ($matches) use ($htmlTags) {
                $quotation = str_replace($htmlTags, '', $matches[1]);
                return "<blockquote>{$quotation}</blockquote>";
            },
            $body
        );
1
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
1
0