6

How to Get Base URL from Full URL String in PHP

 2 years ago
source link: https://www.codexworld.com/how-to/get-base-url-from-full-url-string-in-php/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Home  »  How To Guides  »  PHP   »   How to Get Base URL from Full URL String in PHP

How to Get Base URL from Full URL String in PHP


Base URL is used to create internal web page links dynamically in the website. You can get the base URL from the full URL string using PHP. The parse_url() function helps to parse components from URL in PHP. The base URL can be retrieved from a string using PHP parse_url() function.

The following code snippet shows how to get base URL from URL string with PHP.

  • PHP_URL_SCHEME – This component returns URL scheme (http/https).
  • PHP_URL_HOST – This componenet returns host name (example.com/www.example.com).
$url = 'https://www.codexworld.com/how-to/get-current-url-php/'; 
$url = parse_url($url, PHP_URL_SCHEME).'://'.parse_url($url, PHP_URL_HOST);
$base_url = trim($url, '/');

The above code ($base_url) will return the base URL from given string: https://www.codexworld.com

Leave a reply

Cancel

Comment*

Your Name*

Your Email*

Your Website


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK