admin管理员组

文章数量:1332359

I've seen a few "remove last" slash, but not how to remove both the first and last slash.

I've tried:

$post_url_rel = wp_make_link_relative(get_permalink( $post_id ));       

ltrim($post_url_rel, '/');                  

rtrim($post_url_rel, '/');

But it still says /postname/

Anybody has any idea why this didn't work?

I've seen a few "remove last" slash, but not how to remove both the first and last slash.

I've tried:

$post_url_rel = wp_make_link_relative(get_permalink( $post_id ));       

ltrim($post_url_rel, '/');                  

rtrim($post_url_rel, '/');

But it still says /postname/

Anybody has any idea why this didn't work?

Share Improve this question asked Jun 26, 2020 at 7:54 JoBeJoBe 1712 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

It's because you're not actually putting the result of ltrim() and rtrim() back into the variable. Those functions return the trimmed value, they don't modify the passed variable. So you need to do this:

$post_url_rel = wp_make_link_relative(get_permalink( $post_id ));       
$post_url_rel = ltrim($post_url_rel, '/');                  
$post_url_rel = rtrim($post_url_rel, '/');

Or better yet, just use trim(), which will remove it from both ends:

$post_url_rel = wp_make_link_relative(get_permalink( $post_id ));        
$post_url_rel = trim($post_url_rel, '/');                  

本文标签: customizationRemove slashes (both before and after) in relative post url