Abraham A. Abraham A. - 6 months ago 17
PHP Question

Strip a section of an embed code with PHP

I'm trying to search JW Player embed codes like this one:

<iframe src="//content.jwplatform.com/players/ewQYJ6zA-F6KYzWLn.html" width="480" height="270" frameborder="0" scrolling="auto" allowfullscreen></iframe>


What I need to grab is the code right after
players/
which in this embed would be just
ewQYJ6zA
.

Anyone have an idea how to go about this?

Answer

This will search for players/ followed by one or more letters and numbers.

 <?php
    $string = '<iframe src="//content.jwplatform.com/players/ewQYJ6zA-F6KYzWLn.html" width="480" height="270" frameborder="0" scrolling="auto" allowfullscreen></iframe>';
    preg_match( '~/players/([[a-zA-Z\d]+)~', $string, $matches );
    echo $matches[1];
?>

Output: ewQYJ6zA