It’s late, and I can’t figure out the pattern to the below problem ( which has been simplified ). The problem is with the process function.
<?php
function imcrement_image_id( $template ){
return preg_replace_callback('/%image_id%/', 'process', $template );
}
function process( $match ){
static $count = 0;
$count++;
$output = '';
if( $count %2 == 0){
$output = 'image-' . (--$count);
$count++;
} else {
$output = 'image-' . $count;
}
return $output;
}
?>
The application of the functions is seen in this snippet. I am trying to replace the image_id twice per loop, so that figure id="%image_id" and <a href="%image_id" match.
<?php for ($i=0; $i <3 ; $i++): ?>
<?php ob_start(); ?>
<figure id="%image_id%">
<a href="#%image_id%" >
<img src="http://placehold.it/550x209">
<figcaption>Caption</figcaption>
</a>
<p class="description">Quisque facilisis </p>
</figure>
<?php $template .= ob_get_clean(); ?>
<?php endfor; ?>
// later...
echo $template;
Currently this code does work, but it produces markup like this:
<figure id="image-1">
<a href="#image-1">
...
<figure id="image-3">
<a href="#image-3">
...
<figure id="image-3">
<a href="#image-3">
I’d like it to be image-1 image-1, image-2 image-2, image-3 image-3 and so on. Pointers always appreciated!
Thank you.
Try:
The logic:
$countwill be 2,3,4,5,6… at thereturnstatement$count/2will return 1, 1.5, 2, 2.5, 3…floor($count/2)will return 1, 1, 2, 2, 3…