Remove http and www from form

Discussion in 'PHP' started by rhoula, Dec 24, 2012.

  1. #1
    I have this script that updates a txt file with a website link. I want the script to omit the http:// and www. when the user enters them.

    How can I do that please.

    Here is the code I have so far.

    <?php
    $website = empty($_GET["name"]) ? "" : $_GET["name"];
    if(!$name){$message1 = "Please enter a Website name.";}
    ?>
    <html>
    <head>
    <style>
    .tdr{text-align:right;}
    </style>
    </head>
    <body>
    <form action="store_it.php" method="post">
     <table>
      <tr>
       <td class="tdr">Website:</td>
       <td><input type="text" name="name" value="<?php echo $website; ?>"></td>
       <td>&nbsp;<?php echo $message1; ?></td>
      </tr>
       <tr>
       <td colspan="3" align="center"><input type="submit" value="Submit"></td>
    </body>
    </html>
    PHP:
     
    Solved! View solution.
    rhoula, Dec 24, 2012 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    str_replace(array("http://", "www"), "", $inputString);
     
    EricBruggema, Dec 24, 2012 IP
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    
    <?php
    $message1 = '';
    if (!empty($_GET["name"])) {
        $name = str_ireplace(array('http://', 'www.'), '', $_GET['name']); 
    } else {
        $message1 = 'Please enter a Website name.';
    }
    ?>
    <html>
    <head>
    <style>
    .tdr{text-align:right;}
    </style>
    </head>
    <body>
    <form action="store_it.php" method="post">
     <table>
      <tr>
       <td class="tdr">Website:</td>
       <td><input type="text" name="name" value="<?php echo $website; ?>"></td>
       <td>&nbsp;<?php echo $message1; ?></td>
      </tr>
       <tr>
       <td colspan="3" align="center"><input type="submit" value="Submit"></td>
    </body>
    </html>
    
    PHP:
     
    ThePHPMaster, Dec 24, 2012 IP
  4. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #4
    Thank you so much. I m going to try that out.

    Have a wonderful day and merry Christmas :)
     
    rhoula, Dec 24, 2012 IP
  5. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #5
    I'm sorry it's not working :(

    Just to tell you more about the script. There are 3 files index.php, store_it.php, and domains.txt

    the file index.php is the one that collects the website URL, then store_it.php stores it inside the file domains.txt.

    Please help.

    Thank you
     
    rhoula, Dec 24, 2012 IP
  6. avinash gaud

    avinash gaud Member

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    2
    Trophy Points:
    26
    #6
    Hello,

    Try this


    <?php
    $message1 = '';
    if (!empty($_GET["name"])) {
    $website = str_ireplace(array('http://', 'www.'), '', $_GET['name']);
    } else {
    $message1 = 'Please enter a Website name.';
    }
    ?>
    <html>
    <head>
    <style>
    .tdr{text-align:right;}
    </style>
    </head>
    <body>
    <form action="store_it.php" method="post">
    <table>
    <tr>
    <td class="tdr">Website:</td>
    <td><input type="text" name="name" value="<?php echo $website; ?>"></td>
    <td>&nbsp;<?php echo $message1; ?></td>
    </tr>
    <tr>
    <td colspan="3" align="center"><input type="submit" value="Submit"></td>
    </body>
    </html>
     
    avinash gaud, Dec 24, 2012 IP
  7. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #7
    Still not working

    Here is the store_it.php file in case you need to check it out:

    
    <?php
    $website = empty($_POST["name"]) ? "" : $_POST["name"];
    if(!$website){header("location:index.php?name=$website");}
    else
    {
     $file = "domains.txt";
     $a = fopen($file, "a");
     fwrite($a,$website."\r\n");
     fclose($a);
     $a = fopen($file, "r");
     echo "<table border='1'>";
     echo "<tr><th>Website Name</th></tr>";
     while(!feof($a))
     {
      echo "<tr><td>".fgets($a)."</td></tr>";
      }
     echo "</table>";
     fclose($a);
     }
    ?>
    <form action="index.php">
    <input type="submit" value="Add another">
    </form>
    
    PHP:
     
    rhoula, Dec 25, 2012 IP
  8. avinash gaud

    avinash gaud Member

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    2
    Trophy Points:
    26
    #8
    PHP code

    <?php
    $website = empty($_POST["name"]) ? "" : $_POST["name"];
    $website = str_ireplace(array('http://', 'www.'), '', $website);
    if(!$website){header("location:index.php?name=$website");}
    else
    {
    $file = "domains.txt";
    $a = fopen($file, "a");
    fwrite($a,$website."\r\n");
    fclose($a);
    $a = fopen($file, "r");
    echo "<table border='1'>";
    echo "<tr><th>Website Name</th></tr>";
    while(!feof($a))
    {
    echo "<tr><td>".fgets($a)."</td></tr>";
    }
    echo "</table>";
    fclose($a);
    }
    ?>
    <form action="index.php">
    <input type="submit" value="Add another">
    </form>
     
    avinash gaud, Dec 25, 2012 IP
  9. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #9
    This is what I'm getting now:

    Fatal error: Call to undefined function: str_ireplace() in /homepages/40/d153471067/htdocs/website.com/Alexa/Script/store_it.php on line 3

    Please help
     
    rhoula, Dec 25, 2012 IP
  10. avinash gaud

    avinash gaud Member

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    2
    Trophy Points:
    26
    #10
    Try replacing "str_ireplace" with "str_replace"
     
    avinash gaud, Dec 25, 2012 IP
  11. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #11
    Still the same problem.

    :(
     
    rhoula, Dec 25, 2012 IP
  12. avinash gaud

    avinash gaud Member

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    2
    Trophy Points:
    26
    #12
    wats the error now ??
     
    avinash gaud, Dec 25, 2012 IP
  13. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #13
    Fatal error: Call to undefined function: str_ireplace() in /homepages/40/d153471067/htdocs/website.com/Alexa/Script/store_it.php on line 3
     
    rhoula, Dec 25, 2012 IP
  14. #14
    Replace your code with below code
    <?php
    $website = empty($_POST["name"]) ? "" : $_POST["name"];
    $website = str_replace(array('http://', 'www.'), '', $website);
    if(!$website){header("location:index.php?name=$website");}
    else
    {
    $file = "domains.txt";
    $a = fopen($file, "a");
    fwrite($a,$website."\r\n");
    fclose($a);
    $a = fopen($file, "r");
    echo "<table border='1'>";
    echo "<tr><th>Website Name</th></tr>";
    while(!feof($a))
    {
    echo "<tr><td>".fgets($a)."</td></tr>";
    }
    echo "</table>";
    fclose($a);
    }
    ?>
    <form action="index.php">
    <input type="submit" value="Add another">
    </form>
     
    avinash gaud, Dec 25, 2012 IP
  15. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #15
    Thank you so much. It's working now.

    I really appreciate your help.

    Highly recommended.
     
    rhoula, Dec 25, 2012 IP
  16. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #16
    You look like a great Coder. We will be honored to have you join our list of coders on our website jobsinnorthofamerica.com/freelance

    Once again thank you so much.
     
    rhoula, Dec 25, 2012 IP