前回の質問で解決にしたのですが、思い通りの動作をしていなかったので再度質問します!
前回の質問:https://teratail.com/questions/87971
やりたいこと
phpの正規表現を使ってhtmlのタグを消したい!
php
1//例1 2$html = '1<script>2</script>3<script>4</script>5'; 3$txt = preg_replace(正規表現,'',$html); 4var_dump($txt); //135 ←と表示したい。 5 6//例2 7$html = 'a<script>s</script>b<script>-</script>c'; 8$txt = preg_replace(正規表現,'',$html); 9var_dump($txt); //abc ←と表示したい。
↑が成り立つような正規表現を探しています。
状況
htmlを取得してきて分け合って改行文字を消している。
strip_tagsを使ってhtmlのタグを消しているが
正規表現を使ってこの間を消去する。
php
1//前回の課題 2$html = '1<script>2</script>3<script>4</script>5'; 3$txt = preg_replace('<script.*<\/script>','',$html); 4var_dump($txt); //15 と表示されてしまう。 5 6//前回の解決例 7$html = '1<script>2</script>3<script>4</script>5'; 8$txt = preg_replace('<script>[^(<\/script>)]*<\/script>','',$html); 9var_dump($txt); //135 と表示されてOK! 10 11//今回の課題 12$html = 'a<script>s</script>b<script>-</script>c'; 13$txt = preg_replace('<script>[^(<\/?script>)]*<\/script>','',$html); 14var_dump($txt); //a<script>s</script>bc と表示されてしまう。
どうやら
[^(/?script)]
と書いてはいるが
[^/?script]
と同じ処理をしてるっぽい。
よろしくお願いします!
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/12 08:12
2017/08/12 08:14 編集
2017/08/12 08:21
2017/08/12 08:44