I have been trying to understand how preg_replace_callback works, but I just don't get it.
Say for example, I get_contents from navigation.php. In that are a bunch of a href and divs and I want to give incremental ids to and add in some code commenting before each a href. How would I loop over all those so they would all increment and add the ids and commenting?
<?php
$string = file_get_contents("navigation.php");
$i = 1;
$replace = "<a ";
$with = '<!-- UNIT'.$i.' --><a id=a_'.$i;
$replace2 = "<div ";
$with2 = '<div id=b_'.$i;
preg_replace_callback()
$i++
?>
I figured maybe If I could get an example with my code, maybe i would be able to understand it better
so $replace and $replace2 are my strings I am searching for and $with and $with2 are the replacements respectively, and $i being the increment
so as example of data coming in
<a href="page4.php">Page 4</a>
<a href="page3.php">Page 3</a>
<div class="red">stuff</div>
<div class="blue">stuff</div>
I would want an output like..
<!-- UNIT 1 --><a id="a_1" href="page4.php">Page 4</a>
<!-- UNIT 2 --><a id="a_2" href="page3.php">Page 3</a>
<div id="b_1" class="red">stuff</div>
<div id="b_2" class="blue">stuff</div>