John Davidson

php - Add a string before the last slash in other string

0 comments
Message:


I've pulled the current URL into a variable. So my url is in variable $url.


Example of the $url = 'http://domain.test/en/offers/guided-tours/neighbourhoods/academy-traces-of-the-academy-in-antwerp/'


Now I want to add a new variable ($venueSlug) before the last slash. Example of value from that variable $venueSlug = 'antwerp-city-guided';


The wanted result would be


$newUrl = 'http://domain.test/en/offers/guided-tours/neighbourhoods/antwerp-city-guides/academy-traces-of-the-academy-in-antwerp/'

My code looks like this:


function custom_post_type_link_venue ($urlsub, $post) {

//get venue slug
$venueSlug = get_post_field( 'post_name', $venueId );

if (!empty($venueSlug)) {
if (substr_count($urlsub, $replace) == 0) {
$urlsub = str_replace($s=strrchr($urlsub, '/'), "'/'$venueSlug.$s", $urlsub);

}

}

return $urlsub;
}

But it adds the new variable before every slash:


http://'/'.antwerp-city-guides./'/'.antwerp-city-guides./antwerp.test'/'.antwerp-city-guides./en'/'.antwerp-city-guides./offers'/'.antwerp-city-guides./guided-tours'/'.antwerp-city-guides./neighbourhoods'/'.antwerp-city-guides./academy-traces-o…ademy-in-antwerp'/'.antwerp-city-guides./ ‎


My Answer:
$venueSlug . $s, $urlsub); // Add $venueSlug before the last slash
}
}

return $urlsub;
}

Rate this post

4 of 5 based on 9668 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us