I have quite a few helper functions I have developed and used over the years. Here's some of them (I will keep adding to this as time goes on).
Remove a trailing character(s) from a string:
/**
* Removes trailing character(s) from string
*
* @param (string) $data the string variable to trim
* @param (string) $char the character(s) to trim off the right end
*
* @return (string)
*/
function stripTrailing($data,$char) {
return rtrim($data,$char);
} /* END function */
This helper function came about when I was building queries for SQL. As you build, you add a separator at the end of each element, usually a comma. So, when I finished building a segment, I would need to remove the last comma. And the way to do that, would be:
$query = stripTrailing($query,',');
... or more often, I use a comma followed by a space and want to remove both:
$query = stripTrailing($query,', ');
Clean up a URL
Often when building URLs for your application, you will end up with some discrepancies in how PHP functions return data. For example, $_SERVER['REQUEST_URI'] will return a slash at the beginning of the query string and a slash at the end. It's easy to end up with double slashes in unwanted places of a URL. A straight forward str_replace will not work, since it will also alter the http:// or https:// protocol and mess up your URL. This function handles that.
/**
* Removes double slashes from URL
*
* @param (string) $url The URL to clean up
*
* @return (string) full url
*/
function cleanUrl($url) {
return str_replace(':/','://', trim(preg_replace('/\/+/', '/', $url), '/'));
} /* END function */
To use that, pass a URL variable to the function and it returns a cleaned up version. Example:
$url = 'https://www.testdomain.com//index.php;
$url = cleanUrl($url);
... and echo $url . '<br>'; will return: https://www.testdomain.com/index.php
Get the FULL URL
There just is absolutely no straight forward way to get the URL from the browser. PHP does not provide a function for that. You can build your own.
/**
* Gets full url
*
* @return (string) full url with protocol, domain, port (optional), page (optional), query string (optional)
*/
function getUrl($full=true,$port=false) {
return (
(isset($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? 'https://' : 'http://') .
(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '')) .
(($port) ? ':' . $_SERVER["SERVER_PORT"] : null) .
(($full) ? $_SERVER['REQUEST_URI'] : null)
);
} /* END function */
OK, let's look at this line by line.
The first line gets the protocol. That is going to be either http or https. Note that unlike other functions, I do not examine the port. The port can be set differently that the "standard" with a server configuration and is not reliable.
The second line gets the full domain.
The third line will get the port, if the option is set to true (the option is set to false by default).
The fourth line will get the full query string, if the option is set to true (the option is set to true by default).
Using this page as an example:
$url = getUrl();
will return: https://blog.aprevost.com/posts/helper-functions/
$url = getUrl(false);
will return: https://blog.aprevost.com/
$url = stripTrailing(getUrl(false),'/');
will return: https://blog.aprevost.com