парсинг HTML, чтобы получить только определенные теги - PullRequest
0 голосов
/ 02 мая 2018

Мне нужно разобрать этот HTML-код, чтобы получить только изображение src и текст после тега.

[caption id="attachment_16734" align="aligncenter" width="672"]<a href="http://myeduplus.it/wp-content/uploads/2017/12/consumatori.jpg"><img class="wp-image-16734 size-full" src="http://myeduplus.it/wp-content/uploads/2017/12/consumatori.jpg" alt="" width="672" height="480" /></a> La mucca: consumatore primario o erbivoro[/caption]

1 Ответ

0 голосов
/ 02 мая 2018

Если вы используете следующий код, вы получите только тег изображения и текст рядом с ним.

$input='[caption id="attachment_16734" align="aligncenter" width="672"]<a href="http://myeduplus.it/wp-content/uploads/2017/12/consumatori.jpg"><img class="wp-image-16734 size-full" src="http://myeduplus.it/wp-content/uploads/2017/12/consumatori.jpg" alt="" width="672" height="480" /></a> La mucca: consumatore primario o erbivoro[/caption]';
$str=strip_shortcodes(strip_tags($input, '<img>'));
echo $str;

ПРИМЕЧАНИЕ: здесь я принимаю $ input как переменную со строкой. Это может быть get_the_content () или что-то еще.

Надеюсь, это сработает для вас.

...