php – How can I add a trailing slash to each str_replace result?


I need to adjust some urls on my site.

Currently, the urls are like:

I need to change them to:

I can almost get there with this snippet:

function replace_text($text) {
    $text = str_replace('?query-7e9a-page=", "/page/', $text);
    return $text;
add_filter('the_content', 'replace_text');

but I need a trailing slash at the end of the url.

I thought I could do this:

$text = str_replace('?query-7e9a-page=", "/page/', $text).""; //trailing slash added

but that, and every other stab at this I’ve tried, just adds the slash to the end of the_content. I guess that makes sense, str_replace is processing the entire text string and then adding the /.

Is there a different php function I should be using?

Any suggestions on how I can do this efficiently?

Thanks ahead of time, I haven’t been able to noodle this out,


Chris 2 weeks 2022-09-08T23:32:39-05:00 0 Answers 0 views 0

Leave an answer