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

PHPの変数にHTML要素を格納する

2
Posted at

目的

  • HTMLのtable要素の一連の文字列をPHPの変数に格納する時に若干詰まったので方法をメモ的にまとめておく。

方法

  • ""(ダブルクオート)ではなく''(シングルクオート)でくくるようにする。(""でくくるとHTML要素のクラス名の指定部分などと競合してエラーになる。どうしても""でくくりたいなら文字列として扱うHTML要素の中の""\でエスケープする。)

  • シングルクオートでHTMLの要素をくくる方法を下記に記載する。

    $content = '<table border="1"><tr><th>info_1</th><th>info_2</th></tr><tr><td>Tokyo</td><td>Kanagawa</td></tr><tr><td>Osaka</td><td>Hyougo</td></tr></table>';
    
  • どうしてもダブルクオートでHTMLの要素をくくる時は下記のように要素内の""をエスケープして文字列として変数に格納する。

    $content = "<table border=\"1\"><tr><th>info_1</th><th>info_2</th></tr><tr><td>Tokyo</td><td>Kanagawa</td></tr><tr><td>Osaka</td><td>Hyougo</td></tr></table>";
    
  • 下記方法はNG!!!要素中の""がPHPの文字列認識の""と競合してしまう。

    $content = "<table border="1"><tr><th>info_1</th><th>info_2</th></tr><tr><td>Tokyo</td><td>Kanagawa</td></tr><tr><td>Osaka</td><td>Hyougo</td></tr></table>";
    
2
0
1

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