删除 php 中的标签的一部分

Remove a part of a tag in php

我想从 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

有帮助吗?提前谢谢你。

如果这些是静态字符串,那么正则表达式应该可以工作,但如果您是从互联网某处的网页阅读,我建议使用 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;

preg_match()可以帮到你。

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

print $match[1];

Regex这里

这会有所帮助

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

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

$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', '', $str);

echo $str;

结果

1: Germania
2: FC Schalke 04