Home > Back-end >  php, get src from iframe preg_replace
php, get src from iframe preg_replace

Time:10-10

I have a string that contains iframe

<iframe width="560" height="315" src="https://www.test.com/" title="test" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

how do i use preg_replace to extract the link that contains it; I have to extract the following link 'https://www.test.com/'

obvious by clearing the string from the iframe tag

// I HAVE SOLVED THIS

 $stringaiframe = 'test inserisco ifram tag <iframe width="560" height="315" src="https://www.test.com/" title="test" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
$stringaiframe = preg_replace('/<iframe\s .*?\s src=("[^"] ").*?<\/iframe>/', '""""""$1""""""', $stringaiframe);

CodePudding user response:

I'd go about it with stripos. Find the first occurrence of string 'src="', and from there find the firs ocurrence of '"'. What's in between is your URL.

CodePudding user response:

You can simply split this string.

var str = '<iframe width="560" height="315" src="https://www.test.com/" title="test" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
var src = str.split('src=')[1].split(/[ >]/)[0];
alert("source ===>"   src)

Update If you want this in php use this script.

$str = '<iframe width="560" height="315" src="https://www.test.com/" title="test" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
preg_match('/src="([^"] )"/', $str, $match);
$url = $match[1];

This script will give you the src of iframe. please check and let me know

  • Related