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

Question

I need to adjust some urls on my site.

Currently, the urls are like:

mysite.com?query-7e9a-page=2
mysite.com?query-7e9a-page=3
mysite.com?query-7e9a-page=4

I need to change them to:

mysite.com/page/2/
mysite.com/page/3/
mysite.com/page/4/

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)."https://wordpress.stackexchange.com/"; //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

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

Leave an answer

Browse
Browse