php - Trim url of slash and period -
i trying validate url when sending through form, want make sure begins either https or http, has @ least 1 period, , doesn't have period or trailing slash @ end. have achieved http , period bit through use of regex, can't seem work out how trim url if has either . , ./ , /. or / use rtrim works if finds single characters, there way remove both of them if appear?
here code:
//update 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, '/'); }
you use filter_var
validate url , preg_replace
trim trailing .
, ./
, /.
or /
, if any.
$canonical_url = $_post['canonical_url']; if (filter_var($canonical_url, filter_validate_url, filter_flag_scheme_required)) { $final_canonical = preg_replace('/(\.|\.\/|\/\.|\/)$/', '', $canonical_url); }
note
filter_validate_url
allows rfc 2396 compliant uri, if admit http/https, add:
if(filter_var(…) && strpos($canonical_url, 'http') !== false) {…}
Comments
Post a Comment