I am trying to validate a URL when i am sending it through a form, I want to make sure it begins with either https or http, has at least one period, and then doesn't have a period or trailing slash at the end. I have achieved the http and period bit through the use of regex, however I can't seem to work out how to trim the url if it has either . , ./ , /. or / I use rtrim but it only works if it finds the single characters, is there a way to remove both of them if appear?
Here is my code:
//update the canonical url
$canonical_url = $_POST ['canonical_url'];
$valid_url = preg_match('/^(http[s]?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/', $canonical_url );
if ($valid_url) {
$removed_period = rtrim($canonical_url, '.');
$final_canonical = rtrim($removed_period, '/');
}