Jump to content

  •  

  • iBotModz CBox


    Photo

    Beginning PHP - Lesson 11 - Adv. Strings


    This topic has been archived. This means that you cannot reply to this topic.
    No replies to this topic

    #1 fattwam

    fattwam

      General Grade 2

    • Sub-Admins

    • 3,979 posts
    • Joined: 18-August 07

    Posted 27 March 2008 - 08:19 PM

    Now I want to use these pieces we have discussed to make a couple of interactive scripts. We are going to make a VERY simple one person chat script. I know what your thinking, What good is that!? But this is just to give you some examples of using strings with Loops (Control Structures) and POST. (Later on we will be making a REAL chat script)


    Start your text editor - you should have SciTE (The Scintilla Text Editor) by now. If not you can find it in Downloads
    If not open notepad and type this is into it:


    ";$endofpage = "
    "; // Now that we have a basic layout we can move onto writing the code. /* Second we need to make a statement that checks to see if this is the first time the person has come to this page - or in this case has "Posted" anything to this page. This will evaluate to FALSE the first time the page is run because there will be nothing "Posted" to the page. However, after something has BEEN posted from then on this will be TRUE.*/if (isset($_POST['text'])) { $newtext = $_POST['text']; $oldtext = $_POST['oldtext']; $form2 = "
    (This is the code run from the "If" statement)"; echo $startofpage; echo $form2; echo $endofpage; /* Last, if nothing has been posted to the page, (In other words this is the first visit) this "else" will be TRUE and the following code will run. However, after someone posts something it will be FALSE and the code ABOVE will run.*/ } else { //nothing was posted $firstform = "
    (This is the code run from the "else" statement)"; echo $startofpage; echo $firstform; echo $endofpage;}?>




    Ok, now save it as "post.php" (or whatever you want) and upload it to your server and run it. You will see a square border around a large box, a small box, and a "submit" button. When you type something into the second box and press "submit" what you entered will be posted to the first box along with what was already there. After posting a couple of things and giving yourself a pat on the back for a job well done, come back and lets go over the code.




    First lets look at the starting strings:
     $startofpage ="
    ";

    $endofpage = "
    ";
    ?>


    If you know HTML, (Which you should, if not read the chapters on HTML Before any more PHP lessons) this is just a simple layout. We make a table with one column and one row and center it in the page.




    Next we make the first and ONLY Control Structure - an "if" loop:


    (This is the code run from the "If" statement)"; echo $startofpage; echo $form2; echo $endofpage;?>


    This first loop checks to see if something was posted to that page:

    if (isset($_POST['text']))
    [/code]
    In human language that would be:
    [code]
    if a value in the variable "text" has been set/made


    If something was posted/set/made/whatever we pull it out an put it in a variable:

    $newtext = $_POST['text'];
    $oldtext = $_POST['oldtext'];

    (note: we also get the value from "oldtext")


    Then we take those values and put them in a form and show it to the user again:


    $form2 = "
    (This is the code run from the "If" statement)"; echo $startofpage; echo $form2; echo $endofpage;


    That would end the code that would run.








    However, if nothing was posted to the page, i.e. this is the first visit, the "if" would be FALSE and PHP would move on to the "else" statement:


    (This is the code run from the "else" statement)"; echo $startofpage; echo $firstform; echo $endofpage;}?>




    First we would make a form and put the starting text in "oldtext". Then we would make a field called "text" where the user would answer. So now we have TWO values in this form "oldtext" and "text"
    (note: this is the same as the other form)


    Then all we need to do is print the form to the user and we are done:



    echo $startofpage;
    echo $firstform;
    echo $endofpage;




    Note: I put code below each form so that when you are experimenting with this script you will know which loop has run - "if" or "else".

    Note: If you leave the page and come back, what you have entered will be lost because this script is not saving what you post anywhere. It is just passing the value and the old value back to the page every time it is run.



    So the COMPLETE CODE is this:

    ";$endofpage = "
    ";if (isset($_POST['text'])) { $newtext = $_POST['text']; $oldtext = $_POST['oldtext']; $form2 = "
    (This is the code run from the "If" statement)"; echo $startofpage; echo $form2; echo $endofpage; } else { //nothing was posted $firstform = "
    (This is the code run from the "else" statement)"; echo $startofpage; echo $firstform; echo $endofpage;}?>


    Note: You can download this file below.




    Now that we have a functional script lets do something?s to it! First up, did you notice that every time you post something that the "What is your Name?" text moves over more? This is because there is space between the (This is the code run from the "If" statement)"; echo $startofpage; echo $form2; echo $endofpage;


    This will keep the code clean!

    Now lets suppose that someone is really bored (and boring) and rather than doing something for this world, they waste their time by trying to take down web sites for no reason. So with this form someone could post some kind of malicious code that might redirect to another site (I am sure you can figure out where) or mess-up our layout or something. So now we are going to add another string function to our code that searches the string for any dangerous PHP or HTML characters and removes them.


    if (isset($_POST['text'])) { $newtext = $_POST['text']; $oldtext = $_POST['oldtext']; $newtext = strip_tags(trim($newtext)); //Added Again! $oldtext = strip_tags(trim($oldtext)); //Added Again! $form2 = "
    (This is the code run from the "If" statement)"; echo $startofpage; echo $form2; echo $endofpage;


    Now we trim the code down to the text by removing extra spaces and such with "trim" and then remove any PHP or HTML code that might mess up the page with "strip_tags". What is left is then put in to the variable.



    Extra Addons to the code


    Now lets add some code that prints how many words were posted back to the user. For this we are going to use the "str_word_count" function. We can also add "strlen" to print out how many characters are in the posted comment.



    if (isset($_POST['text'])) { $newtext = $_POST['text']; $oldtext = $_POST['oldtext']; $newtext = strip_tags(trim($newtext)); $oldtext = strip_tags(trim($oldtext)); $form2 = "
    (This is the code run from the "If" statement)"; echo $startofpage; echo $form2;// Just Added echo "You Posted "; echo str_word_count($newtext); echo " words and "; echo strlen($newtext); echo "Characters.";// Just Added echo $endofpage;


    Here we passed the variable "$newtext" that holds what the user posted to the function str_word_count which then returned the number of word in the variable.



    Now we are going to add a function called "ord" that echo?s the first characters ASCII value. This only serves as an example of more things you can to your strings.


    if (isset($_POST['text'])) { $newtext = $_POST['text']; $oldtext = $_POST['oldtext']; $newtext = strip_tags(trim($newtext)); $oldtext = strip_tags(trim($oldtext)); $form2 = "
    (This is the code run from the "If" statement)"; echo $startofpage; echo $form2; echo ""; //Added to center the text echo "You Posted "; echo str_word_count($newtext); echo " words and "; echo strlen($newtext); echo "Characters."; echo "Here is the ASCII Value for the first character you posted: "; //Added echo ord($newtext); //Added echo ""; //Added to center the text echo $endofpage;



    This Fun addon will print the users post backwards!


    echo $startofpage;
    echo $form2;

    echo "";

    echo "You Posted ";
    echo str_word_count($newtext);
    echo " words and ";
    echo strlen($newtext);
    echo "Characters.";

    echo "Here is the ASCII Value for the first character you posted: ";
    echo ord($newtext);

    echo "Here is your text Backwards: "; //Added
    echo strrev($newtext); //Added

    echo "";
    echo $endofpage;




    You can also build a bad-word filter to keep your posts clean! For that we can use the function "str_replace" which works like this:

     
    $bodytag = str_replace("%body%", "black", "");
    // Provides:
    ?>




    if (isset($_POST['text'])) {
    $newtext = strip_tags(trim($_POST['text']));
    $oldtext = strip_tags(trim($_POST['oldtext']));

    $badwords = array("sucks", "damn", "#!@?", "ASP"); //Added
    $newtext = str_replace($badwords, "****", "$newtext"); //Added



    You can add more words to the filter, just put a "," after each one.

    Ok, well that is it for this script, but don't worry we will make a real chat script soon enough. However this was good practice using strings and posts.

    If you want to do more with this script you can visit PHP.net - Strings for more functions and examples of how to use them.

    You can also download the complete "post.php" file below. I incourage you to try to add more things to this script maybe something that makes all of the text entered LOWER CASE.
    You can download the file here.