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 1 year has passed since last update.

【PHP】指定した文字列を切り取りpreg_match_allを使う

Last updated at Posted at 2023-04-27

プレグマッチオール

コード

<?php
$contents = 'ナルト<h3>aaa</h3>サスケ<h3>bbb</h3>サクラ<h3>ccc</h3>チョウジ';
preg_match_all('/<h3>(.*?)<\/h3>/', $contents, $h3);
var_dump($h3);

結果

array(2) {
  [0]=>
  array(3) {
    [0]=>
    string(12) "<h3>aaa</h3>"
    [1]=>
    string(12) "<h3>bbb</h3>"
    [2]=>
    string(12) "<h3>ccc</h3>"
  }
  [1]=>
  array(3) {
    [0]=>
    string(3) "aaa"
    [1]=>
    string(3) "bbb"
    [2]=>
    string(3) "ccc"
  }
}

コード

<?php
$contents = 'ナルト<h3 clsss="">aaa</h3>サスケ<h3 id="">bbb</h3>サクラ<h3>ccc</h3>チョウジ';
preg_match_all('/<h3(.*?)>(.*?)<\/h3>/', $contents, $h3);
var_dump($h3);

結果

array(3) {
  [0]=>
  array(3) {
    [0]=>
    string(12) "<h3>aaa</h3>"
    [1]=>
    string(12) "<h3>bbb</h3>"
    [2]=>
    string(12) "<h3>ccc</h3>"
  }
  [1]=>
  array(3) {
    [0]=>
    string(0) ""
    [1]=>
    string(0) ""
    [2]=>
    string(0) ""
  }
  [2]=>
  array(3) {
    [0]=>
    string(3) "aaa"
    [1]=>
    string(3) "bbb"
    [2]=>
    string(3) "ccc"
  }
}
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?