PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 20, 2022

[FIXED] How to replace certain div with preg_replace?

 November 20, 2022     php, preg-replace     No comments   

Issue

I have this code HTML:

<div class="wrap">
<div class="wp-checkbox" data-value="zf">content</div>
<div class="wp-checkbox" data-value="tdp">content</div>
</div>

I would like to replace the content of these divs, but each differently.

I tried to do it by code:

$pattern = '/=\"zf\">.*<\/div>/';
$pattern2 = '/=\"tdp\">.*<\/div>/';
$html = preg_replace( $pattern, '="zf">new content of div</div>', $html );
$html = preg_replace( $pattern2, '="tdp">new content of another div</div>', $html );

But there is a problem that this code - it replaced all my divs and I get this:

<div class="wrap">
<div class="wp-checkbox" data-value="zf">new content of div</div>
</div>

How can I change these divs one by one? How can I show in the pattern that I want to change only until the first closing </div> (not to last one)?

I would like to get this result:

<div class="wrap">
<div class="wp-checkbox" data-value="zf">new content of div</div>
<div class="wp-checkbox" data-value="tdp">new content of div</div>
</div>

Thank you in advance for help.


Solution

You really shouldn't try to use regex to parse HTML. Instead, use the DOMDocument class to process the HTML:

$doc = new DOMDocument();
$doc->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($doc);
$div = $xpath->query('//div[@data-value="zf"]')[0];
$div->nodeValue = "new content of div";
$div = $xpath->query('//div[@data-value="tdp"]')[0];
$div->nodeValue = "new content of another div";
echo $doc->saveHTML();

Output:

<div class="wrap">
<div class="wp-checkbox" data-value="zf">new content of div</div>
<div class="wp-checkbox" data-value="tdp">new content of another div</div>
</div>

Demo on 3v4l.org

If you must use regex, you just need to correct your patterns to make them non-greedy by adding ? after the .*:

$pattern = '/=\"zf\">.*?<\/div>/';
$pattern2 = '/=\"tdp\">.*?<\/div>/';
$html = preg_replace( $pattern, '="zf">new content of div</div>', $html );
$html = preg_replace( $pattern2, '="tdp">new content of another div</div>', $html );

Output:

<div class="wrap">
<div class="wp-checkbox" data-value="zf">new content of div</div>
<div class="wp-checkbox" data-value="tdp">new content of another div</div>
</div>

Demo on 3v4l.org



Answered By - Nick
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing