Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dsk52/751da4846382e3c9c6760c6705fa63ba to your computer and use it in GitHub Desktop.

Select an option

Save dsk52/751da4846382e3c9c6760c6705fa63ba to your computer and use it in GitHub Desktop.

特殊な環境で、PCとSPで若干中身違うんだけど、メンテナンスの面を考えると同じファイルで管理したいよねってなったときにやりました

いっけな〜い altつっけてないや☆ PHPでつけちゃお☆みたいな時に正規表現でreplaceかけるのをこんな感じにやればできたって話 ※この設定はフィクションです 正規表現は毎回手探りなのでメモ

<img src="/image/hoge.jpg">

$data に上のタグを読み込んでるていの話。 file_get_contents() とか fopen() あたりでファイルを読み込んで変数に持ってる状態

<?php
$procecced_data = preg_replace(
    '/<img (.*?)>/',
    '<img $1 alt="">',
    $data );
?>

第1引数で指定した (.*?) の中身は $1 で取得できるって話を初めて知った。
数字に関しては1スタートで、 () の1個目が1, 2個目が2って感じで変えれる。
なお、第2引数の方は正規表現の // は要らないっぽい。

たどり着くまでに参考にした

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment