TecTao Posted June 4, 2014 Share Posted June 4, 2014 I want to remove the HTTP:// or HTTPS:// from the insert into the DB. The Viewer may put just www, or copy the entire URL with the HTTP:// or HTTPS:// I tried using the preg-replace but couldn't seem to get it to work. Any other recommendation? Thanks in advance, Mike Quote Link to comment https://forums.phpfreaks.com/topic/288979-trouble-removing-certain-letters-from-post-prior-to-insert/ Share on other sites More sharing options...
cyberRobot Posted June 4, 2014 Share Posted June 4, 2014 If you post the code you tried, someone may be able to help fix it. Alternatively, have you looked into parse_url()? http://www.php.net/manual/en/function.parse-url.php Note that I personally haven't used the function before...and it may be overkill for what you're trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/288979-trouble-removing-certain-letters-from-post-prior-to-insert/#findComment-1481845 Share on other sites More sharing options...
Psycho Posted June 4, 2014 Share Posted June 4, 2014 Alternatively, have you looked into parse_url()? http://www.php.net/manual/en/function.parse-url.php I was not aware of that function. @TecTao: You need to determine how broad you want this process to be. If you only want to strip out "http://" or "https://" from the input string, then you shoudl use a string function as opposed to RegEx. Of course, you would want it to be case insensitive. $search = array('http://', 'https://'); $replace = array('', ''); $url = str_ireplace($search, $replace, $_POST['url']); However, if you want to capture other types of exceptions, such as "//domain.com/path/page.php", then the above function that CyberRobot referenced appears to be the way to go. You would use the returned values for host, path and query then concatenate them together to get the URL from those parts. Quote Link to comment https://forums.phpfreaks.com/topic/288979-trouble-removing-certain-letters-from-post-prior-to-insert/#findComment-1481847 Share on other sites More sharing options...
Jacques1 Posted June 4, 2014 Share Posted June 4, 2014 (edited) Calling parse_url() function will not return the expected result. The problem is that most users and many programmers don't understand how an URL actually looks like. Contrary to popular belief, “www.google.com” does not refer to the website of Google. It's a relative URL consisting only of a relative path. For example, if you're currently on https://yoursite.com/some/path/, then “www.google.com” refers to https://your-site.com/some/path/www.google.com/ And that's why parse_url() will interpret the string as a path. If you want to interpret the URLs in a different way to match popular misconceptions, you'll have to take a different approach. First of all: What do you do with URLs which use a scheme other than HTTP or HTTPS like mailto:foo@example.com or ftp://some-site.com/some/file? Leave them as they are? Remove them? Rewrite them? What about URLs which are clearly supposed to be a relative path like /images/kitten.jpg? Leave them as they are? Resolve them relative to the current site? Rewrite them? Edited June 4, 2014 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/288979-trouble-removing-certain-letters-from-post-prior-to-insert/#findComment-1481856 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.