マイコンピュータの「input.txt」テキストファイルから入力を受け取り、結果を「output.txt」テキストファイルに出力するように次のスクリプトを適用しようとしています。
スクリプトはインターネットからHTMLを取得するのにうまく機能しますが、必要な適応を特定することはできません。
奇妙なことは、私が1年前にそれを見つけたことです。しかし、私が何をしたのか覚えていません。私はプログラマーではありません。
パスワード:
url='http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags'
curl -s "$url" |
sed -Ene:n -etD \
-e's/ ?[^ "]*"[^"]*"//g;/"/'bN \
-e's/[[:space:]]*($|<)/\n\1/' \
-e'/^Moderator.s Note/q' \
-e'/.\n/P;/\n</!t' -e:D \
-e'/\n/D;/^<script>/!s/>/&\n/' \
-e'/\n/!s/<\/script>/\n/' -e:N \
-e'/\n/!{N;s///;}' -e//tD -etn
ベストアンサー1
HTMLファイルからJavaScriptを削除してプレーンテキストを保持する方法は?
これは、正規表現を使用してトークンとメンテナンスの可能性を解析することに関連する別の問題を強調すると考えるので、興味深い質問です。
このスクリプトは、システムでPHPを使用できる場合にこれを行います。
#!/usr/local/bin/php
# point the #! to wherever your PHP commandline binary is
<?php
error_reporting(1);
$html = file_get_contents('http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags');
// create an object representing the DOM tree of the webpage
$document = new DOMDocument;
$document->loadHTML($html);
// store the <script> elements as a DOMN
$script_nodes = $document->getElementsByTagName('script');
// For some reason you can't use the DOMNode::removeChild method
// when iterating through an instance of PHP's DOMNodeList
// so use an array to queue the values in. see
// http://php.net/manual/en/domnode.removechild.php
$scripts_to_remove = [];
for ( $i=0; $i < $script_nodes->length; $i++ ) {
$scripts_to_remove[] = $script_nodes->item($i);
}
// now we can iterate through the <script> nodes removing them
foreach ( $scripts_to_remove as $s_node ) {
$parent = $s_node->parentNode;
$parent->removeChild($s_node);
}
// print out the new DOM as HTML
echo $document->saveHTML();
使用法
スクリプトを使用するには、上記のコードを含むファイルを設定して実行可能にし、タグを削除したHTMLを含める必要があるファイルに実行し、出力をリダイレクトします<script>
。