-1

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.

Fiorelo Odobashi
  • 121
  • 1
  • 1
  • 11
  • Are the 1: and 2: line numbers or are they included in the string? We need some more information, where do you get the string, is it static ... – PHPhil Jul 25 '15 at 08:48
  • you can try this `preg_replace('@(\d+: ).*title="([a-zA-Z0-9 ]+)".*@i', '$1$2', $text)` or `preg_replace('@(\d+: ).*title="([^"]+)".*@i', '$1$2', $text)` – manjeet Jul 25 '15 at 08:54
  • yes, they are included in a string. – Fiorelo Odobashi Jul 25 '15 at 08:56
  • thank you @anonymous for your answer, i don't know how to use the first and the second part of preg_replace --> '@(\d+: ).*title="([a-zA-Z0-9 ]+)".*@i', '$1$2' -- i search on the internet but i don't really understand it. – Fiorelo Odobashi Jul 25 '15 at 09:04

4 Answers4

1

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;
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
0

preg_match() can help you.

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

print $match[1];

Regex here

MH2K9
  • 11,951
  • 7
  • 32
  • 49
0

this will help

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

In every string take starting digits and value of title attribute

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

echo $str;

result

1: Germania
2: FC Schalke 04
splash58
  • 26,043
  • 3
  • 22
  • 34