2

1 つの文字列で HTML タグを削除し、1 つのタイプのみを保持する必要があります。これを含む文字列が 1 つあります。

 <!-- comment -->   <div id="55"> text </div> <span name=annotation value=125> 2 text </span> <p id="55"> text 3</p><span>text 4 <span>

そして私はこれが必要です:

text  <span name=annotation value=125> 2 text </span> text 3text4

したがって、このフォームを持つものを除くすべての HTML タグを削除する必要があります

"/(<span[^>]*annotation[^>]*value=.?(\w*).?[^>]*>)(.*?)<\/span>/"

これを別の表現の一部として使用しますが、アイデアを得るために

これどうやってするの?

でできることはわかっていますpreg_replace()が、必要なパターンがわかりません。

例:

$str='<!-- comment --><p><b>Deoxyribonucleic acid</b> (<b>DNA</b>) is 
   a molecule encoding the <a href="/wiki/Genetics" title="Genetics">genetic</a> instructions
   used in the development and functioning of all known living <a href="/wiki/Organism" title="Organism">organi
   sms</a> and many <a href="/wiki/Virus" title="Virus">viruses</a>. Along with <a href="/wiki/RNA" title="RNA">RNA</a> and <a href="/wiki/Proteins" title="Proteins" class="mw-redirect">proteins</a>, DNA is one of the three major 
   <a href="/wiki/Macromolecules" title="Macromolecules" class="mw-redirect">macromolecules</a> 
   that are essential for all known forms of <a href="/wiki/Life" title="Life">life</a>.
   Genetic information is encoded<span id="200120131815150" 
   class="mymetastasis" value="247" name="annotation"> as a sequence of nucleotides (</span><a href="/wiki/Guanine" title="Guanine"><span id="200120131815151" class="mymetastasis" value="247" name="annotation">
   guanine</span></a><span id="200120131815152" class="mymetastasis" value="247" name="annotation">, </span><a href="/wiki/Adenine" title="Adenine"><span id="200120131815153" class="mymetastasis" value="247"
   name="annotation">adenine</span></a><span id="200120131815154" class="mymetastasis" value="247" name="annotation">,
   </span><a href="/wiki/Thymine" title="Thymine"><span id="200120131815155" class="mymetastasis" value="247"
   name="annotation">thymine</span></a><span id="200120131815156" class="mymetastasis" value="247" name="annotation">, 
   and </span><a href="/wiki/Cytosine" title="Cytosine">
   <span id="200120131815157" class="mymetastasis" value="247" name="annotation">cytosine</span></a><span id="200120131815158" class="mymetastasis" value="247" name="annotation">) 
   recorded using the letters G, A, T, and C. Most DNA molecules are double-strande</span>d helices, consisting of two long <a href="/wiki/Polymers" title="Polymers" class="mw-redirect">polymers</a> of simple units called <a href="/wiki/Nucleotide" 
   title="Nucleotide">nucleotides</a>, molecules with <a href="/wiki/Backbone_chain" title="Backbone chain">backbones</a>
   made of alternating <a href="/wiki/Monosaccharide" title="Monosaccharide">sugars<
   /a> (<a href="/wiki/Deoxyribose" title="Deoxyribose">deoxyribose</a>) and <a href="/wiki/Phosphate"
   title="Phosphate">phosphate</a> groups (related to phosphoric acid), with the <a href="/wiki/Nucleobases" title="Nucleobases" class="mw-redirect">nucleobases</a> (G, A, T, C) attached to the sugars. DNA is well-suited for biological information storage, since the DNA backbone is resistant to cleavage and the double-stranded structure provides the molecule with a 
   built-in duplicate of the encoded information.</p>';

PD: 改行、タブなどは意図的ではありません。ソーステキストの一部。

4

2 に答える 2

3

これを行うには複数必要ですregex

作業コード:

<?php
    header("Content-Type: text/plain");

    $str = '<!-- comment -->   <div id="55"> text </div> <span name=annotation value=125> 2 text </span> <p id="55"> text 3</p><span>text 4 </span>';

    // Save needed values
    $str = preg_replace("/<(span[^>]*?annotation.*?)>(.*?)<\/(.*?)>/", "!!!$1!!!$2!!!$3!!!", $str); 

    // Remove everything else
    $re = "/(<[^>]*?>)/";
    $str = preg_replace($re, "", $str);

    // Restore
    $str = preg_replace("/\!\!\!(span[^>]*?annotation.*?)\!\!\!(.*?)\!\!\!(.*?)\!\!\!/", "<$1>$2</$3>", $str); 

    echo $str;
?>

出力:

text  <span name=annotation value=125> 2 text </span>  text 3text 4 

入力:

$str='<!-- comment --><p><b>Deoxyribonucleic acid</b> (<b>DNA</b>) is 
   a molecule encoding the <a href="/wiki/Genetics" title="Genetics">genetic</a> instructions
   used in the development and functioning of all known living <a href="/wiki/Organism" title="Organism">organi
   sms</a> and many <a href="/wiki/Virus" title="Virus">viruses</a>. Along with <a href="/wiki/RNA" title="RNA">RNA</a> and <a href="/wiki/Proteins" title="Proteins" class="mw-redirect">proteins</a>, DNA is one of the three major 
   <a href="/wiki/Macromolecules" title="Macromolecules" class="mw-redirect">macromolecules</a> 
   that are essential for all known forms of <a href="/wiki/Life" title="Life">life</a>.
   Genetic information is encoded<span id="200120131815150" 
   class="mymetastasis" value="247" name="annotation"> as a sequence of nucleotides (</span><a href="/wiki/Guanine" title="Guanine"><span id="200120131815151" class="mymetastasis" value="247" name="annotation">
   guanine</span></a><span id="200120131815152" class="mymetastasis" value="247" name="annotation">, </span><a href="/wiki/Adenine" title="Adenine"><span id="200120131815153" class="mymetastasis" value="247"
   name="annotation">adenine</span></a><span id="200120131815154" class="mymetastasis" value="247" name="annotation">,
   </span><a href="/wiki/Thymine" title="Thymine"><span id="200120131815155" class="mymetastasis" value="247"
   name="annotation">thymine</span></a><span id="200120131815156" class="mymetastasis" value="247" name="annotation">, 
   and </span><a href="/wiki/Cytosine" title="Cytosine">
   <span id="200120131815157" class="mymetastasis" value="247" name="annotation">cytosine</span></a><span id="200120131815158" class="mymetastasis" value="247" name="annotation">) 
   recorded using the letters G, A, T, and C. Most DNA molecules are double-strande</span>d helices, consisting of two long <a href="/wiki/Polymers" title="Polymers" class="mw-redirect">polymers</a> of simple units called <a href="/wiki/Nucleotide" 
   title="Nucleotide">nucleotides</a>, molecules with <a href="/wiki/Backbone_chain" title="Backbone chain">backbones</a>
   made of alternating <a href="/wiki/Monosaccharide" title="Monosaccharide">sugars<
   /a> (<a href="/wiki/Deoxyribose" title="Deoxyribose">deoxyribose</a>) and <a href="/wiki/Phosphate"
   title="Phosphate">phosphate</a> groups (related to phosphoric acid), with the <a href="/wiki/Nucleobases" title="Nucleobases" class="mw-redirect">nucleobases</a> (G, A, T, C) attached to the sugars. DNA is well-suited for biological information storage, since the DNA backbone is resistant to cleavage and the double-stranded structure provides the molecule with a 
   built-in duplicate of the encoded information.</p>';

出力:

Deoxyribonucleic acid (DNA) is 
   a molecule encoding the genetic instructions
   used in the development and functioning of all known living organi
   sms and many viruses. Along with RNA and proteins, DNA is one of the three major 
   macromolecules 
   that are essential for all known forms of life.
   Genetic information is encoded<span id="200120131815150" 
   class="mymetastasis" value="247" name="annotation"> as a sequence of nucleotides (</span>
   guanine<span id="200120131815152" class="mymetastasis" value="247" name="annotation">, </span><span id="200120131815153" class="mymetastasis" value="247"
   name="annotation">adenine</span>,
   <span id="200120131815155" class="mymetastasis" value="247"
   name="annotation">thymine</span>, 
   and 
   <span id="200120131815157" class="mymetastasis" value="247" name="annotation">cytosine</span>) 
   recorded using the letters G, A, T, and C. Most DNA molecules are double-stranded helices, consisting of two long polymers of simple units called nucleotides, molecules with backbones
   made of alternating sugars (deoxyribose) and phosphate groups (related to phosphoric acid), with the nucleobases (G, A, T, C) attached to the sugars. DNA is well-suited for biological information storage, since the DNA backbone is resistant to cleavage and the double-stranded structure provides the molecule with a 
   built-in duplicate of the encoded information.
于 2013-01-20T09:31:55.540 に答える
1

PHP には、strip_tagsと呼ばれる組み込み関数があります。

span タグのみを保持したい場合は、2 番目の引数を使用します。

そう、

$cleanString = strip_tags($dirtyString, '<span>');
于 2013-01-20T05:15:40.997 に答える