アプリ版:「スタンプのみでお礼する」機能のリリースについて

下記の通り、aタグ内からのみ

「<FONT style='background-color:yellow;'><b>」

「</b></FONT>」

を削除したいです。


<FONT style='background-color:yellow;'><b>▲</b>
<a href="■<FONT style='background-color:yellow;'><b>▲</b></FONT>●" target="_blank">
■<FONT style='background-color:yellow;'><b>▲</b>●</a>

 ↓

<FONT style='background-color:yellow;'><b>▲</b>
<a href="■▲●" target="_blank">
■<FONT style='background-color:yellow;'><b>▲</b>●</a>



■▲●は毎回内容が変わります。

■●は文字が無い場合もあります。

どのように記述すれば良いでしょうか。

参考になるサイトURlをご提示頂く回答でも構いません。

助けて下さい。

宜しくお願い致します。



※FONTはそっとしておいてください

A 回答 (3件)

$strに変換前の文字列が格納れているとして



$result = preg_replace("/(.*)(<a href=\".*?)(<FONT style='background-color:yellow;'><b>)(.*?)(<\/b><\/FONT>)(.*target=\"_blank\">.*)/m","$1$2$4$6",$str);

で如何でしょうか。
$resultに変換結果が格納されます。
以下、試験したコードです。
-------------------------------------
<?php
$str = file_get_contents("test.html");
$result = preg_replace("/(.*)(<a href=\".*?)(<FONT style='background-color:yellow;'><b>)(.*?)(<\/b><\/FONT>)(.*target=\"_blank\">.*)/m","$1$2$4$6",$str);
print $result;
?>
------------------------------
test.htmlの内容
cat test.html
<FONT style='background-color:yellow;'><b>▲</b>
<a href="■<FONT style='background-color:yellow;'><b>▲</b></FONT>●" target="_blank">
■<FONT style='background-color:yellow;'><b>▲</b>●</a>
--------------------
実行結果
php goo1.php
<FONT style='background-color:yellow;'><b>▲</b>
<a href="■▲●" target="_blank">
■<FONT style='background-color:yellow;'><b>▲</b>●</a>
    • good
    • 0
この回答へのお礼

ありがとうございます!

希望通りに結果になりました。

たった1行で出来るんですね!
すごいです。

正規表現を覚えたら、いかに便利か改めて感じました。
頑張って勉強してゆきたいと思います。

勉強になりました。
ありがとうございました。

お礼日時:2018/09/18 18:46

回答者No1です。


PHPの話だったのですね。見落としていました。すみません。

PHPであれば、
preg_replace($pattern, $replacement, $string);
を使って、回答No1の検索・置換条件を設定します。

この場合、注意する点は
<や"などの前、要所要所にエスケープ「\」を入れなければならないところです。
こちらではすぐには検証できないのでヒントだけですみません。

置換$replacementではPHPの場合、$1$2$3という書き方になります。
こちらの設定でも適宜エスケープが必要になるかと。
    • good
    • 0

完璧ではないかもしれませんが、素直にそのまま書くと



検索
<a href="(.*?)<FONT style='background-color:yellow;'><b>(.*?)</b></FONT>(.*?)" target="_blank">

置換
<a href="\1\2\3" target="_blank">


検索条件の●▲■の部分に
任意の文字列 .*? を置きます。
後でその文字を代入するためグループとして()で囲っておきます。

置換の\1は検索でグループとして()で括った1個目という意味です。
使うソフトによって、書き方が違うかもしれませんので、
ダメだったら調べてみてください。($1とする場合もあります)
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!