Loony

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Loony

    To replace a part of the string with another string we will use str_replace function in PHP. This function will replace all the occurrence of the string we target and will replace with the string we supply. Here is an example.

    Code:
    <?
    $string="This is an example on how html is used in 
    developing user-friendly web sites. By using HTML  we can
     control the content of a page";
    
    echo $string;
    echo "
    
    ";
    $string=str_replace("html","PHP",$string);
    
    echo "[b]After replace[/b] 
    
    $string";
    
    ?>
    If you run the above code your will see result like this 
    
    
    
    This is an example on how html is used in developing user-friendly web sites. By using HTML we can control the content of a page
    
    After replace
    
    This is an example on how PHP is used in developing user-friendly web sites. By using HTML we can control the content of a page
    
    
    
    So you can see the text html is replace by PHP but HTML is not replaced. 
    So str_replace() is a case sensitive string replacing function.
    We can use str_ireplace() to get case insensitive string replacement

    Code:
    The main difference between str_replace and str_ireplace is str_replace is a case sensitive string replacement and str_ireplace is a case in-sensitive string search and replacement. 
    
    str_ireplace function in PHP when applied to a main string, it searches for occurrence of a string and replace them by another string. 
    
    This function can take an array as search string and each element of the array if present is replaced by another string. We can try with an example. If you are searching for a simple example then you can see str_replace function. Here is a sample code which takes an array of search words and replaces them by another word within a string. 
    
    $search = array("good", "php", "html", "ASP");
    $final_string = str_replace($search, "*", "All good sites uses PHP along with HTML or they use ASP and html");
    echo $final_string;
    
    
    As we have seen here we can use an array as search terms and all the elements of the array can be replaced by a replace string. We can also use another array as replacement array and each element is replace by matching element from replacement array, if not element is found then blank string is replaced. Here is the script code for that 
    
    $search = array("good", "php", "html", "asp");
    $replace=array("GOOD","PHP","HTML","ASP","NO");
    $final_string = str_replace($search, $replace, "All good sites uses PHP along with HTML or they use asp and html");
    echo $final_string;
    Visit: Chat4u.mobi - The New Lay Of being a site of your dreams!
    Visit: WapMasterz Coming Back Soon!
    _______
    SCRIPTS FOR SALE BY SUBZERO
    Chat4u Script : coding-talk.com/f28/chat4u-mobi-script-only-150-a-17677/ - > Best Script for your site no other can be hacked by sql or uploaders.
    FileShare Script : coding-talk.com/f28/file-wap-share-6596/ -> Uploader you will never regret buying yeah it mite be old now but it still seems to own others...
    _______
    Info & Tips
    php.net
    w3schools.com

    #2
    that helps me a little im tring to script my new chatroom and i wana add commands in chat like ban=loony will ban loony but i cant get it to ban how would i use the string in a mysql command

    Comment


      #3
      Dont know if this might help

      <div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'><?php
      $IP = $_SERVER[&#39;REMOTE_ADDR&#39;];
      $IP_array = file(&#39;../folder/IP.dat&#39;);
      $IP_array = str_replace("\n", "", $IP_array);
      $IP_array = str_replace("\r", "", $IP_array);
      foreach ($IP_array as $IP_test) {
      (substr_count($IP, $IP_test) != "0") {

      echo &#39;Banned World!&#39;;

      die();
      }
      }

      echo &#39;Hello World!&#39;;

      ?php></div>

      Comment


        #4
        first of all mate

        make a line of them test one out of one
        Visit: Chat4u.mobi - The New Lay Of being a site of your dreams!
        Visit: WapMasterz Coming Back Soon!
        _______
        SCRIPTS FOR SALE BY SUBZERO
        Chat4u Script : coding-talk.com/f28/chat4u-mobi-script-only-150-a-17677/ - > Best Script for your site no other can be hacked by sql or uploaders.
        FileShare Script : coding-talk.com/f28/file-wap-share-6596/ -> Uploader you will never regret buying yeah it mite be old now but it still seems to own others...
        _______
        Info & Tips
        php.net
        w3schools.com

        Comment


          #5
          This function is implemented in mysql and requires 3 fields, replace(coulumn name , search, replace) and more faster than php’s str_replace(). Bacause while using str_replace() you need to fetch each field from mysql database and replace searched string in php and then update the field in mysql database. if you use this function you only need to run single sql query.

          <div class='sqltop'>SQL</div><div class='sqlmain'>[color=blue;font-weight:bold]UPDATE[/color] `table` SET `coulumn` = REPLACE(`coulmn`,’search’,&#39;replace’); </div>

          Comment


            #6
            ive tryed about 20 difrent ways but cant seem to get it to work

            Comment


              #7
              Well then I&#39;m just as lost as you in this case

              Comment


                #8
                <div class='quotetop'>QUOTE </div>
                that helps me a little im tring to script my new chatroom and i wana add commands in chat like ban=loony will ban loony but i cant get it to ban how would i use the string in a mysql command[/b]
                ok here we go
                like ban/username
                Code:
                $checkmesage = explode("/",$msg);
                if ($checkmesage[0]=="ban")
                {
                $againstuser=$checkmesage[1];
                $queryaction = mysql_query("update `usertable` set ban=&#39;wateveurban&#39; where login=&#39;$againstuser&#39;;");
                }

                Comment


                  #9
                  that dont work either
                  Code:
                  $name = mysql_real_escape_string($_GET[&#39;name&#39;]);
                                   $message = mysql_real_escape_string($_POST[&#39;message&#39;]);
                      $room = mysql_real_escape_string($_POST[&#39;roomname&#39;]);
                      $time = time();
                      $name = mysql_real_escape_string($_GET[&#39;name&#39;]);
                      addonline($username);
                          $checkmessage = explode("/",$message);
                  if ($checkmesage[1]=="ban")
                  {
                  $againstuser=$checkmesage[1];
                  mysql_query("UPDATE &#39;users&#39; set level_access=&#39;6&#39; where username=&#39;$againstuser&#39;;");
                  
                  }

                  Comment


                    #10
                    script and sql might help lol

                    Comment


                      #11
                      lol no need for sql but the snippet is above

                      Comment


                        #12
                        dude atleast copy things properly
                        Code:
                        <?php
                        $name = mysql_real_escape_string($_GET[&#39;name&#39;]);
                        $message = mysql_real_escape_string($_POST[&#39;message&#39;]);
                        $room = mysql_real_escape_string($_POST[&#39;roomname&#39;]);
                        $time = time();
                        addonline($username);
                        $checkmessage = explode("/",$message);
                        if ($checkmesage[0]=="ban")
                        {
                        $againstuser=$checkmesage[1];
                        $ban = mysql_query("UPDATE &#39;users&#39; set level_access=&#39;6&#39; where username=&#39;$againstuser&#39;;");
                        }
                        ?>

                        Comment


                          #13
                          i changed that see if it worked cause it didnt work the other way

                          Comment


                            #14
                            it dont work

                            Comment


                              #15
                              i mean the code above is perfect so there must be something your doing wrong when adding it to your script or sql so without seeing them its kinda hopless trying to help :P

                              Comment

                              Working...
                              X