Untitled UI logotext
Solutions
WebsitesEcommerceMobile AppsWeb AppsProduction Support & Maintenance
Our work
Company
About usBlogPodcastContact us
Book a free consultation

How do I remove a domain from a string in PHP?

Olivia Rhye

This doesn't feel like it should be that hard, and luckily it's not! However, some of the more common ways I found had been commented on being rather unreliable. Most people recommend an easy str_replace, removing the specified domain and replacing it with an empty string. But what if you don't know your domain yet, what if your still in development? No one wants to go back and change that...

So here's a rather brilliant little combo of exploding and replacing that will work regardless of what the domain is. Done and done.

$fileurl = get_field('pdf');
$fileurlexplode = explode('.com/', $fileurl);
$removedomain = $fileurlexplode [0].'.com/';
$fileurl = str_replace($removedomain , "",$fileurl); ?>

Ready to start a project?

Book a free consultation
Untitled UI logotext
Our work
About us
Blog
Careers
Submit a ticket
Agency Partnerships
Contact
© 2024 fjorge. All rights reserved.
Privacy