Why should you use a legitimate DOM parser instead of regex -- even for such a minor string manipulation?
Because regex is not "DOM-aware" -- it will treat a substring that isn't a tag as if it was a tag just because it resembles a tag.
Because your input may change slightly with or without your consent.
Because your required string manipulation may grow in complexity as your application matures.
Because using dedicated tools for the tasks they were designed to tackle, makes you appear to be a careful, considered, and professional IT craftsman/craftswoman.
First, a loop of iframe nodes using only DOM parser followed by a url parser, then substr_replace()
to inject the 's' without removing any of the original characters.
Code: (Demo)
$html = <<<HTML
<p>Some random text <iframe src="http://some-random-link.com" width="425" height="350" frameborder="0"></iframe></p>
HTML;
$dom = new DOMDocument;
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
foreach ($dom->getElementsByTagName('iframe') as $iframe) {
$src = $iframe->getAttribute('src');
if (parse_url($src, PHP_URL_SCHEME) === 'http') {
$iframe->setAttribute('src', substr_replace($src, 's', 4, 0));
}
}
echo $dom->saveHTML();
Alternatively, you can target the qualifying src
attributes with XPath.
Code: (Demo)
$html = <<<HTML
<p>Some random text <iframe src="http://some-random-link.com" width="425" height="350" frameborder="0"></iframe>
<iframe src="https://cant-touch-this.com" width="425" height="350" frameborder="0"></iframe>
</p>
HTML;
$dom = new DOMDocument;
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
foreach ($xpath->query("//iframe[starts-with(@src, 'http') and not(starts-with(@src, 'https'))]/@src") as $src) {
$src->nodeValue = substr_replace($src->nodeValue, 's', 4, 0);
}
echo $dom->saveHTML();
Not only will these techniques be more reliable than regex, the syntax to these parsers is far more readable by humans and will make your script much easier to manage over time.