Separate a text by IFrames in PHP

0

I am trying to separate a text HTML containing iframes from a preg_split , and it does not come out. What I need is to create a array with texts e iframes . For example ['texto', 'iframe', 'iframe', 'texo'] .

What I have done has been this:

<?
php 
    $html = '<span style="font-size:14px;"><iframe allowfullscreen="" frameborder="0" height="360" mozallowfullscreen="" src="https://player.vimeo.com/video/34234234" webkitallowfullscreen="" width="640"></iframe></span></p>
        <p>
            <span style="font-size:14px;"><a href="https://vimeo.com/34234234"> Texto.</a> from <a href="https://vimeo.com/user7990284">Texto</a> on <a href="https://vimeo.com">Vimeo</a>.</span></p>
        <p>';

    $separacionIframes = preg_split('/(<iframe[^>]+>)/i', $html, -1, PREG_SPLIT_DELIM_CAPTURE);

    print_r($separacionIframes);
?>

What it paints me is a array but like this: ['texto', 'iframe + texto'] and it should be ['texto', 'iframe', 'texto] .

I do not know if I have explained myself well, any questions you may have ask me. Thank you very much!

    
asked by Csc99 07.10.2018 в 13:25
source

1 answer

0

Try the following: preg_split("/(<iframe[^>]*>.*?<\/iframe>)/is", $html, null, PREG_SPLIT_DELIM_CAPTURE);

Demo: link

Result:

Array
(
    [0] => <span style="font-size:14px;">
    [1] => <iframe allowfullscreen="" frameborder="0" height="360" mozallowfullscreen="" src="https://p...content-available-to-author-only...o.com/video/34234234" webkitallowfullscreen="" width="640"></iframe>
    [2] => </span></p>
        <p>
            <span style="font-size:14px;"><a href="https://v...content-available-to-author-only...o.com/34234234"> Texto.</a> from <a href="https://v...content-available-to-author-only...o.com/user7990284">Texto</a> on <a href="https://v...content-available-to-author-only...o.com">Vimeo</a>.</span></p>
        <p>
)
    
answered by 05.11.2018 в 02:20