php - How to remove a double Forward Slash from the end of a URL if there is one -
i need removing double slash url entered user. wordpress front end.
i have option users input link. once input/paste link in value field, need add affiliate code @ end of link , show final output (full link) affiliate id.
this code used.
<form onsubmit="return false" oninput="txtfullurl.value = txturl.value +'/affiliateid'"> url : <input type="text" name="txturl" /> <br><br> full url : <input type="text" name="txtfullurl" > <br><br> </form>
the problem url entered user might or might not have "/" (forward slash) @ end.
therefore if link has double forward slash before affiliate id, need remove one.
how can this?
you should doing sort of sanitising or validation on server. flexible solution make use of wp_parse_url
function, used in conjunction http_build_query
.
example
$url = 'https://www.google.com//somethingelse'; $parsed_url = wp_parse_url($url); $query_string = http_build_query([ 'affiliate_id' => 'my_id', ]); $affiliate_link = $parsed_url['scheme'] . '://' . $parsed_url['host'] . '/?' . $query_string;
Comments
Post a Comment