|
-
Jul 15th, 2008, 11:45 PM
#1
Thread Starter
Hyperactive Member
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  from  authoritative  sources  showing  the ', ' 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";
}
-
Jul 16th, 2008, 02:25 AM
#2
Re: preg_replace causing to repeat
The while loops is making it repeat. What exactly do you regular expressions do?
-
Jul 16th, 2008, 07:45 AM
#3
Thread Starter
Hyperactive Member
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);
-
Jul 16th, 2008, 08:28 AM
#4
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 . 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?
-
Jul 16th, 2008, 08:35 AM
#5
Thread Starter
Hyperactive Member
Re: preg_replace causing to repeat
 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 . 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 ;.
-
Jul 16th, 2008, 01:20 PM
#6
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
-
Jul 16th, 2008, 01:55 PM
#7
Thread Starter
Hyperactive Member
Re: preg_replace causing to repeat
PHP Code:
$new .= preg_replace("/[^ -þ]/", "", $old);
This one caused it to repeat.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|