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
4

4 Answers 4

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

return str_replace($invalid, '', $_POST[$key]); not work.
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

Comments

0

use parse_url:

$url_part = parse_url($url);

$new_url = $url_part['host'] . $url_part['path'] . $url_part['query'] . $url_part['fragment'];

Comments

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);
?>

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.