Удалите все теги html из строки, оставив только один из них. Расширенные strip_tags с одним атрибутом идентификатора

Мне нужно удалить теги HTML в одной строке и оставить только один их тип. У меня есть одна строка, содержащая это:

 <!-- 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: разрывы строк, табуляция и т. д. непреднамеренны. Часть исходного текста.


person Martin    schedule 19.01.2013    source источник
comment
Не используйте регулярное выражение для анализа HTML. Используйте парсер XML.   -  person hsz    schedule 20.01.2013
comment
Я просто быстро просмотрел ваш вопрос, поэтому не знаю, поможет ли это. psoug.org/snippet/Convert-HTML-to-plain-text_36. htm   -  person evan.stoddard    schedule 20.01.2013


Ответы (2)


Для этого вам понадобится несколько 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.
person ATOzTOA    schedule 20.01.2013
comment
спасибо, но мне это нужно в php, у меня была эта ошибка Undefined offset: 1 в этой строке $out .= $sub_matches[1];. Вы знаете, что это может случиться - person Martin; 20.01.2013
comment
Опубликуйте точную строку, которую вы используете сейчас. Возможно, вам не хватает закрывающего тега в последнем теге span. - person ATOzTOA; 20.01.2013
comment
текст, который можно использовать, например, представляет собой содержимое html в теге html с id=content этой страницы en.wikipedia .org/wiki/DNA, добавив ‹span name=annotation› в любом месте текста, текст должен выйти чистым, если этот тег - person Martin; 20.01.2013
comment
Вам нужны HTML-комментарии? - person ATOzTOA; 20.01.2013
comment
@Martin На странице, на которую вы ссылаетесь, нет тегов annotation. Я обновил свой код, чтобы удалить комментарии. Попробуй. - person ATOzTOA; 20.01.2013
comment
@ATOzTOA ах да, извините, случайно получил страницу, чтобы сделать идею, но, конечно, могут быть комментарии к строке, приносим извинения за неудобства. - person Martin; 20.01.2013
comment
@Martin Решение не будет работать, если у вас есть вложенные элементы. Вам нужно решение в Javascript/jquery? - person ATOzTOA; 20.01.2013
comment
@ATOzTOA Я обновил вопрос и добавил один пример, вы можете протестировать свой код здесь [writecodeonline.com/php/] . Мне нужно было, чтобы это было в php. Но если это не может работать в php, я найду лучшее решение. - person Martin; 20.01.2013
comment
@Martin, я обновил свой ответ полностью функциональным кодом ... наслаждайтесь! - person ATOzTOA; 20.01.2013
comment
@Martin Может быть, вам следует изменить название на более общее. - person ATOzTOA; 21.01.2013
comment
@ATOzTOA спасибо, я уже пробовал, но не придумал лучшего. Любые идеи? - person Martin; 21.01.2013
comment
Этот выглядит нормально, давайте оставим его там :) - person ATOzTOA; 21.01.2013

В PHP для этого есть встроенная функция, которая называется strip_tags.

Если вы хотите сохранить только тег span, используйте второй аргумент.

So,

$cleanString = strip_tags($dirtyString, '<span>');
person Ares    schedule 20.01.2013
comment
OP нужен конкретный span, а не все диапазоны. - person ATOzTOA; 20.01.2013