Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What are the problem?

posted at

updated at

Organization

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

わきゃ!!

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
        );
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
0
Help us understand the problem. What are the problem?