Search code examples
phphtmltagsattributesstrip

Remove a part of a tag in php


I want to remove tags from php to show this results:

Before:

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>

After

1: Germania
2: FC Schalke 04

Any helps? Thank you in advance.


Solution

  • If these are static strings then a regex should work but if you are reading from a webpage somewhere on the interwebs I would suggest using DOMDocument.

    As you are reading the data as a string this might be of interest? It doesn't remove anything from the string data - just finds the element attributes that you are looking for and echoes them back.

                $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;