This is what I have so far, but whatever I try, my album-content
won't get truncated with substr()
.
$gallery = preg_replace('/(<div class="album-content">)(.*?)(<\/div>)/e', "'$1' . substr(\"$2\", 0, 50) . '$3'", $gallery);
UPDATE:
Turns out my class name was incorrect, and my regex does seem to work. I only had to do another str_replace() to un-escape the output.
$gallery = preg_replace('/(<div class="album-desc">)(.*?)(<\/div>)/e', "'$1' . substr(\"$2\", 0, 50) . '$3'", $gallery);
$gallery = str_replace('\"album-desc\"', '"album-desc"', $gallery);
Output before modifying:
<a href="..." class="album">
<div class="album-content"><p class="album-title">Album 1</p>
<div class="album-desc"><p>This will be truncated by substr().</p></div>
</div>
<img src="..." />
</a>
Thanks all, anyways!
Use preg_replace_callback()
$replace = preg_replace_callback('/(asd)f/i', function($matches) {
$matches[1] = substr($matches[1], 0, 2);
return $matches[1];
}, 'asdf');
Full documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With