Remove slashes (both before and after) in relative post url

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, "https://wordpress.stackexchange.com/");                  
$post_url_rel = rtrim($post_url_rel, "https://wordpress.stackexchange.com/");

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