Results 1 to 7 of 7

Thread: preg_replace causing to repeat

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    447

    preg_replace causing to repeat

    I think the preg_replace is causing the former result to repeat:
    INSERT INTO correction_nbsp (bwas_pages_id, content_before, content_after) VALUES (702, 'chapter: ', ' CHAPTER 1 chapter: ')
    INSERT INTO correction_nbsp (bwas_pages_id, content_before, content_after) VALUES (711, 'copies &nbspfrom &nbspauthoritative &nbspsources &nbspshowing &nbspthe ', ' CHAPTER 1 chapter: copies from authoritative sources showing the ')
    INSERT INTO correction_nbsp (bwas_pages_id, content_before, content_after) VALUES (714, 'told what the truth is, and still not see it. ', ' CHAPTER 1 chapter: copies from authoritative sources showing the told what the truth is, and still not see it. ')
    INSERT INTO correction_nbsp (bwas_pages_id, content_before, content_after) VALUES (716, 'see it? ', ' CHAPTER 1 chapter: copies from authoritative sources showing the told what the truth is, and still not see it. see it? ')
    PHP Code:
    while($row mysql_fetch_array($result)){
            
    $old preg_replace('#&?nbsp;?#i'' '$row['content']);
            
    $new .= preg_replace("/[^ -þ]/"""$old);
            
    $sql_record2 "INSERT INTO correction_nbsp (".$acronym."_pages_id, content_before, content_after)
            VALUES ("
    .$row['id'].", '".$row['content']."', '".$new."')";
            
    mysql_query($sql_record2,$con) or die(mysql_error());
            echo 
    $sql_record2."<br />\n";
        } 
    Compare bible texts (and other tools):
    TheWheelofGod

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: preg_replace causing to repeat

    The while loops is making it repeat. What exactly do you regular expressions do?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    447

    Re: preg_replace causing to repeat

    What do you mean by regular expressions?
    This code intended to:

    1. select the records where there are nbsp both with and without the & and ; in the database table;
    2. replace them with a regular space bar by updating the records;
    3. insert these records in a new table making it easy to undo in case there are any errors in the process of undoing.

    But I blocked the UPDATE string to test the INSERT string.
    Here's the rest of the code:
    PHP Code:
    <?php 
    include("../files/constants.php");
    include(
    "../files/dbconnection.php");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    mysql_select_db($acronym."_book"$con);
    $sql "SELECT * FROM ".$acronym."_pages WHERE content LIKE '%nbsp%'";
    $result mysql_query($sql) OR exit( 'Error: ' mysql_error() );
        
    //echo $sql;
        
    $constructed = array();
    $sQry "SHOW TABLES FROM ".$acronym."_book";
    $qry mysql_query($sQry) or die(mysql_error());
    while (
    $tables mysql_fetch_row($qry))
    {
        
    $constructed[] = $tables[0];
    }

    if (!
    in_array("correction_nbsp"$constructed))
    {
        
    //to keep record of what changes are made I decided to create a table
            
    $sql_record "CREATE TABLE correction_nbsp
            (
            cor_ID int(6) NOT NULL AUTO_INCREMENT,
            PRIMARY KEY(cor_ID),
            "
    .$acronym."_pages_id int(6),
            content_before LONGBLOB,
            content_after LONGBLOB
            )"
    ;
            
    mysql_query($sql_record,$con) or die(mysql_error());
    }    
        

        
        
        echo 
    "<span style='font-weight: bold;'>".$sql_record."</span><br />\n";
        while(
    $row mysql_fetch_array($result)){
            
    $old preg_replace('#&?nbsp;?#i'' '$row['content']);
            
    $new .= preg_replace("/[^ -þ]/"""$old);
            
    //if($row['content'] != ""){
                //echo $new."<br />\n";
                
    $sql_record2 "INSERT INTO correction_nbsp (".$acronym."_pages_id, content_before, content_after)
                VALUES ("
    .$row['id'].", '".$row['content']."', '".$new."')";
                
    mysql_query($sql_record2,$con) or die(mysql_error());
                echo 
    $sql_record2."<br />\n";
            
    //}
            //$sql2 = "UPDATE ".$acronym."_pages SET content = '".preg_replace('/&?nbsp;?/', ' ', $row['content'])."' WHERE id = {$row['id']} LIMIT 1"; 
            //$sql2 = "UPDATE ".$acronym."_pages SET content = '".$new."' WHERE content = '".$row['content']."'";
            //mysql_query($sql2,$con) or die(mysql_error());
            //echo $sql2."<br />\n";
        
    }
    mysql_close($con);
    Compare bible texts (and other tools):
    TheWheelofGod

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: preg_replace causing to repeat

    I am not sure why you want to replace nbsp without an ampersand or comma. The HTML standard dictates that it should only be interpreted as a non breaking space if it is in the form &nbsp;. Anyhow, the html_entitiy_decode() function will do that for you and other HTML special characters.

    A regular expression is what preg_replace uses to match sctirns within strings. Regular expressions are often use to match string patterns rather than a single fixed string.

    As to why your loop is repeating, it must be to do with the rows returned by the query. Are you sure there are no duplicates?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    447

    Re: preg_replace causing to repeat

    Quote Originally Posted by visualAd
    I am not sure why you want to replace nbsp without an ampersand or comma. The HTML standard dictates that it should only be interpreted as a non breaking space if it is in the form &nbsp;. Anyhow, the html_entitiy_decode() function will do that for you and other HTML special characters.

    A regular expression is what preg_replace uses to match sctirns within strings. Regular expressions are often use to match string patterns rather than a single fixed string.

    As to why your loop is repeating, it must be to do with the rows returned by the query. Are you sure there are no duplicates?
    I see that the preg_replace mentioned the 2nd time in my code had a .= with it which caused it to repeat.

    Can you tell me what else or where to find the list of things html_entitiy_decode() can decode? But as for the nbsp... I had converted a pdf book to html. Either the conversion or the effort of using the replace function in Dreamweaver messed some things up. So some of the nbsps had & and ; missing. That's why my effort was to find all with and without & and ;.
    Compare bible texts (and other tools):
    TheWheelofGod

  6. #6
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: preg_replace causing to repeat

    I don't see any regular expressions with .= in them and I still don't see how that would cause it to repeat
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    447

    Re: preg_replace causing to repeat

    PHP Code:
    $new .= preg_replace("/[^ -þ]/"""$old); 
    This one caused it to repeat.
    Compare bible texts (and other tools):
    TheWheelofGod

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width