0

I want remove http:// and https:// form all of url in my form submit. So I used below code to remove http:// and make next process. If I used something with http:// in input its not work and go to my error page.

Now I want to direct remove http:// or https:// form all of submit url. I also read here about str_replace and preg_replace but cannot understand how to apply this here.

$web = mysqli_real_escape_string($dbh, $_POST['web']);
$fb = mysqli_real_escape_string($dbh, $_POST['fb']);
$tw = mysqli_real_escape_string($dbh, $_POST['tw']);
$gg = mysqli_real_escape_string($dbh, $_POST['gg']);


$keys = array('web','fb','tw','gg');

$invalid_strings = array("http://","https://");


   foreach($keys as $key) {    # iterate through each key to check
      foreach($invalid_strings as $invalid) {   # iterate through each invalid string
         if(strpos($_POST[$key], $invalid) === 0) {
     return str_replace($invalid, '', $_POST[$key]);
         }
      }
   }

   // Update process query
Community
  • 1
  • 1
koc
  • 955
  • 8
  • 26
  • http://stackoverflow.com/questions/4357668/how-do-i-remove-http-https-and-slash-from-user-input-in-php – Jagadeesh Aug 01 '15 at 10:30
  • http://stackoverflow.com/questions/9364242/how-to-remove-http-www-and-slash-from-url-in-php – Jagadeesh Aug 01 '15 at 10:31
  • http://webtuts.way2tutorial.com/remove-the-http-https-wwwand-slashes-from-url-in-php/ Refer these links – Jagadeesh Aug 01 '15 at 10:31
  • Possible duplicate of [How do I remove http, https and slash from user input in php](https://stackoverflow.com/questions/4357668/how-do-i-remove-http-https-and-slash-from-user-input-in-php) – Madbreaks Sep 07 '17 at 21:00

4 Answers4

0

return str_replace($invalid, '', $_POST[$key]);

Mike Miller
  • 3,071
  • 3
  • 25
  • 32
0

You'll want to replace return with $errors[] =

The return statement will end the current loop and leave your array empty. Performing $array[] = will append the new value to the end of the array

0

use parse_url:

$url_part = parse_url($url);

$new_url = $url_part['host'] . $url_part['path'] . $url_part['query'] . $url_part['fragment'];
Al Amin Chayan
  • 2,460
  • 4
  • 23
  • 41
0

uncomment first 3 lines if you directly run this page

<?php
//$_POST['ra']='http://small-seo-tools.in';
//$_POST['rah']='http://rahul.com';
//$_POST['rau']='https://rahulr.com';
print_r($_POST);
    foreach($_POST as $key=>$value){
        $_POST[$key] = str_replace('https://','',$_POST[$key]);
        $_POST[$key] = str_replace('http://','',$_POST[$key]);
    }
echo"<br><br>";
print_r($_POST);
?>
rahul chaurasia
  • 153
  • 1
  • 9
  • This code will first search for "https://" and if not find then search "http://" if we make it opposite then it will not work perfectly because "http://" will replaced from "https://" except "s". Thanks and don't forget to upvote if you ind it helpful – rahul chaurasia Aug 01 '15 at 10:28