【问题标题】:Remove a part of a tag in php在php中删除标签的一部分
【发布时间】:2015-07-25 08:43:56
【问题描述】:

我想从 php 中删除标签以显示此结果:

之前:

1: <span class="n n21" title="Germania">&nbsp;</span>
2: <a href="/team/34?hl=it-IT" title="FC Schalke 04"><img data-src="http://2015.sofifa.org/15/teams/24/34.png" class="list-mini" src="http://2015.sofifa.org/15/teams/24/34.png"></a>

之后

1: Germania
2: FC Schalke 04

有什么帮助吗?提前谢谢你。

【问题讨论】:

  • 1: 和 2: 是行号还是包含在字符串中?我们需要更多信息,你从哪里得到字符串,是静态的吗...
  • 你可以试试这个preg_replace('@(\d+: ).*title="([a-zA-Z0-9 ]+)".*@i', '$1$2', $text)preg_replace('@(\d+: ).*title="([^"]+)".*@i', '$1$2', $text)
  • 是的,它们包含在一个字符串中。
  • 谢谢@anonymous 的回答,我不知道如何使用 preg_replace 的第一和第二部分 --> '@(\d+: ).*title="([a -zA-Z0-9 ]+)".*@i', '$1$2' -- 我在网上搜索,但我不太明白。

标签: php html tags attributes strip


【解决方案1】:

如果这些是静态字符串,那么正则表达式应该可以工作,但如果你是从互联网上某处的网页阅读,我建议使用 DOMDocument。

当您将数据作为字符串读取时,这可能会引起您的兴趣吗?它不会从字符串数据中删除任何内容 - 只是找到您正在寻找的元素属性并将它们回显。

            $data='
            <span class="n n21" title="Great Britain">&nbsp;</span>
            <span class="n n21" title="Germania">&nbsp;</span>
            <span class="n n21" title="france">&nbsp;</span>
            <a href="/team/34?hl=it-IT" title="FC Schalke 04"><img data-src="http://2015.sofifa.org/15/teams/24/34.png" class="list-mini" src="http://2015.sofifa.org/15/teams/24/34.png"></a>
            <a href="/team/35?hl=it-IT" title="Porto"><img data-src="http://2015.sofifa.org/15/teams/24/35.png" class="list-mini" src="http://2015.sofifa.org/15/teams/24/34.png"></a>
            <a href="/team/36?hl=it-IT" title="England"><img data-src="http://2015.sofifa.org/15/teams/24/34.png" class="list-mini" src="http://2015.sofifa.org/15/teams/24/34.png"></a>';


            libxml_use_internal_errors( true );
            $dom = new DOMDocument('1.0','utf-8');
            $dom->validateOnParse=false;
            $dom->standalone=true;
            $dom->preserveWhiteSpace=true;
            $dom->strictErrorChecking=false;
            $dom->substituteEntities=false;
            $dom->recover=true;
            $dom->formatOutput=true;

            $dom->loadHTML( $data );

            $parse_errs=serialize( libxml_get_last_error() );
            libxml_clear_errors();

            /* get titles from SPAN elements */
            $col=$dom->getElementsByTagName('span');
            foreach( $col as $node ) echo $node->getAttribute('title').'<br />';
            /* Get titles from A tags */
            $col=$dom->getElementsByTagName('a');
            foreach( $col as $node ) echo $node->getAttribute('title').'<br />';

            $dom=null;

【讨论】:

【解决方案2】:

preg_match()可以帮到你。

$html = '<span class="n n21" title="Germania">&nbsp;</span>';
$pattern = '/title="(.+)"/';
preg_match($pattern, $html, $match);

print $match[1];

Regex这里

【讨论】:

    【解决方案3】:

    这会有所帮助

    preg_replace('#<span.*?\s+title="([^"]+)">&nbsp;.*?<a\s+.*?title="([^"]+)"><img#sui', "$1\n$2", text);
    echo nl2br($text);
    

    【讨论】:

      【解决方案4】:

      在每个字符串中取起始数字和标题属性的值

      $str = '1: <span class="n n21" title="Germania">&nbsp;</span>
      2: <a href="/team/34?hl=it-IT" title="FC Schalke 04"><img data-src="http://2015.sofifa.org/15/teams/24/34.png" class="list-mini" src="http://2015.sofifa.org/15/teams/24/34.png"></a>';
      
      $str = preg_replace('/^(\d+:\s).+\stitle=\"([^\"]+)\".+$/m', '\1\2', $str);
      

      回声 $str;

      结果

      1: Germania
      2: FC Schalke 04
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-10
      • 1970-01-01
      • 2013-08-19
      • 2011-08-17
      • 1970-01-01
      • 2013-12-25
      • 2017-05-11
      相关资源
      最近更新 更多