I’m somewhat new to PHP, so this will probably sound stupid, but how you best rewrite the below code to remove the leading http:// and/or www. off of a url if they are present?
function jt_cmb_validate_text_url( $new ) {
if ( '' == $new ) { return; }
if ( !preg_match('/http:\/\//', $new) ) {
$new = 'http://' . $new;
}
return $new;
}
As you can see, the code adds the leading http:// to the url if it isn’t already present, but as I’m trying to remove both the http:// and www.
Would you go with four if statements (one if none are present, one each if only a single thing is present and another for when both are present)?
Any help would be much appreciated. 🙂
To ensure that you don’t remove any more than you need to (for instance, there could be a full URL in the query string you don’t want to remove), you should use a regex: