how to insert characters into a string at a certain point in php?

Viewed 40

I am customizing a wordpress plugin so that an image loading dynamically into the background of a div. I have the code for the image url as

$instance['imageurl'] which resolves to something like

http://url-here.../wp-content/uploads/2021/06/IMG_6227-copy.jpg

however, I'd like to alter this to add the same few characters in every instance, always just before the ".jpg" or whatever the file type may be, so that in this instance it would be

http://url-here.../wp-content/uploads/2021/06/IMG_6227-copy-519x416.jpg

How can I code this?

Here's the code in this section:

<a class="sal-feature" href="<?php echo $instance['link']?>"><div class="row sal-feature-wrap" style="background-repeat:no-repeat; background-size:100% auto; background-image: url('<?php echo $instance['imageurl']; ?>');">

Thanks for your help!

1 Answers

<a class="sal-feature" href="<?php echo $instance['link']?>"><div class="row sal-feature-wrap" style="background-repeat:no-repeat; background-size:100% auto; background-image: url('<?php echo substr_replace($instance['imageurl'], "YOUR_TEXT", strrpos($instance['imageurl'],"."), 0); ?>');">

strrpos gets the index of the last '.'

substr_replace will insert your text at the given index

Related