Adblock breaks this site

Manipulation

Discussion in 'Web Programming' started by Lil Wayne, Jun 29, 2007.

  1. Lil Wayne

    Lil Wayne Guest

    Referrals:
    2
    Manipulation

    [​IMG]


    I forgot to put php in the title, sowwy.


    Before we get started make yourself conformable.​


    What to do with the "nn" in cannon

    If you know the input word "cannon" and just want to extract the characters "nn" it is very simple to do it using substri().

    This code:

    <?php
    //set string equal to Funny
    $str = 'Funny';

    // extract the "nn" and print
    echo substr($str, 2, 2);
    ?>

    renders this:
    nn
    Finding the pattern "nn" in any word.
    For this we turn to something called Regular Expressions, regex for short. Regex is a wonderful way of searching for specific patterns of characters. There are whole books on the subject, and it can be a very tough topic to initially break into, but once mastered, even at a basic level, you can get tremedous results. In this example, we test for the pattern - one or more numbers or letters / "nn" / one or more letters or numbers - in five words and return true (which is the value of 1 if it is present) or false (0) if it is not.
    <?php
    $str = 'cannon';
    $str1 = 'Monney';
    $str2 = 'Bunny';
    $str3 = 'Banana';
    $str4 = 'nnotgoingtowork';


    $pattern =
    "/([a-zA-Z0-9])+nn([a-zA-Z0-9_-])+/";

    // returns true
    echo preg_match($pattern, $str);
    print ' ' . $str . ' because has character(s)/nn/character(s)<br />';
    echo preg_match($pattern, $str1);
    print ' ' . $str1 . ' because has character(s)/nn/character(s)<br />';
    echo preg_match($pattern, $str2);
    print ' ' . $str2 . ' because has character(s)/nn/character(s)<br />';
    echo preg_match($pattern, $str3);
    print ' ' . $str3 . ' FALSE because has NO "nn"<br />';
    echo preg_match($pattern, $str4);
    print ' ' . $str4 . ' FALSE because has NO character before "nn"<br />';
    ?>


    Produces:

    1 cannon because has character(s)/nn/character(s)

    1 Monney because has character(s)/nn/character(s)
    1 Bunny because has character(s)/nn/character(s)
    0 Banana FALSE because has NO "nn"
    0 nnotgoingtowork FALSE because has NO character before "nn"

    String Replacement Function


    You can do a string replacement easily. The following code replaces ALL instances of the characters nn:
    <?
    $str = "...this if not Funny any more. It is nnot going to have any nn's anny more ...";

    // returns the string without nn pattern ... substitute xx
    echo str_replace("nn", "xx", $str);
    ?>

    So we get xx's instead of nn's:​
     
  2. MellowYellow

    MellowYellow Guest

    Referrals:
    0
    Manipulation

    Finally a tutorial that's not a beginner's HTML guide. It's nice for people completely confused about parsing text, ie making highscore grabbers and other tools.

    You should had just made your search pattern /.+nn.+/i. Simpler and would give you better results unless you really wanted your character class to only contain common letters and numbers. :p
     
< Delphi | Help with HTML. Serious. >


 
 
Adblock breaks this site