Jump to content

Trouble Removing Certain Letters from Post Prior to Insert


TecTao

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Jacques1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.