简体   繁体   中英

preg_replace img src to data-src stack overflow (PHP)

I using lazyload script for iframes I need to make a preg_replace code to change src to data-src.

I try something like this but I failed:

$cache = preg_replace('%<iframe.*?src=["\'](.*?)["\'].*?/?>%i', 'data-src="$1"', $content);

My code is only print data-src="the link" without the full iframe code.

New Answer that uses legitimate DOM parsering functions to reliably mutate valid html:

  • Iterate all iframe tags.
  • Insert the new data-src attribute using the existing src attribute.
  • Remove the old src attribute.
  • Print the updated DOM.

As mentioned by @user706420, removing the src attribute from the <iframe> tag is a bad decision because it renders the html invalid. My answer is demonstrative on how to execute a tag attribute replacement generally, but I agree with @user706420 that this task does seem logically flawed.

Code: ( Demo )

$html = <<<HTML
<p>Some random text <iframe src="the link"" width="425" height="350" frameborder="0"></iframe></p>
HTML;

libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
foreach ($dom->getElementsByTagName('iframe') as $iframe) {
    $iframe->setAttribute('data-src', $iframe->getAttribute('src'));
    $iframe->removeAttribute('src');
}
echo $dom->saveHTML();

Output:

<p>Some random text <iframe width="425" height="350" frameborder="0" data-src="the link"></iframe></p>

Old Answer (improved on Oct 9, 2020) with advice that I no longer endorse because regex is "DOM-ignorant"...

Match the start of the <iframe and all characters within the opening tag until you encounter a space character which is followed immediately by the substring src= -- this ensures that the targeted src= substring doesn't have any preceding non-white-space characters (IOW, it is a whole/solitary word).

The substring before the space must be released/forgotten -- this is what \\K does. The space will need to be consumed and replaced with data- .

Code: ( Demo )

$content = 'Some text that contains src <iframe src="www.example.com"/> Some text';
echo preg_replace('~<iframe[^>]*\K (?=src=)~i', ' data-', $content);

Output:

Some text that contains src <iframe data-src="www.example.com"/> Some text

Although I have improved the regex, valid html strings can see be intentionally written to break the regex like: <iframe src="www.example.com"/ data-type="<iframe" data-whoops=" src= oh my"> For this reason, I ONLY recommend parsing html with a dom parser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM