Reputation: 659
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!
Upvotes: 6
Views: 1929
Reputation: 360782
it'd have to be
..., "'$1'" . substr("$2", 0, 50) . "'$3'", ...
with the substr "outside" of the string.
Upvotes: 0
Reputation: 2707
Use preg_replace_callback()
$replace = preg_replace_callback('/(asd)f/i', function($matches) {
$matches[1] = substr($matches[1], 0, 2);
return $matches[1];
}, 'asdf');
Upvotes: 4