替换mysql表中x和y之间的值以及特殊字符


Replacing values between x and y in a mysql table as well as special characters

我需要从特定的mysql列中删除价格信息。价格可以是54.99美元或54.99美元

我已经有了一个脚本,可以一次替换一个值,例如,我可以用空格替换54.99美元,但有数百个不同的值需要替换。我该如何让我的脚本删除所有数字、美元符号和文本"美元"?

举个例子:我有一个名为book的专栏,典型的一行中有这样的文字:"这本书很棒,售价54.99美元"

我需要删除"美元"answers"54.99",这样剩下的文字就会变成:"这本书很棒,而且很贵"

我使用的脚本如下:

// code here
if($showErrors) {
    error_reporting(E_ALL);
    ini_set('error_reporting', E_ALL);
    ini_set('display_errors',1);
}

$MJCONN = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database,$MJCONN);

$table_sql = 'SHOW TABLES';
$table_q = mysql_query($table_sql,$MJCONN) or die("Cannot Query DB: ".mysql_error());
$tables_r = mysql_fetch_assoc($table_q);
$tables = array();
do{
    $tables[] = $tables_r['Tables_in_'.strtolower($database)];
}while($tables_r = mysql_fetch_assoc($table_q));

$use_sql = array();
$rowHeading = ($queryType=='replace') ? 
        'Replacing '''.$search.''' with '''.$replace.''' in '''.$database."''n'nSTATUS    |    ROWS AFFECTED    |    TABLE/FIELD    (+ERROR)'n"
      : 'Searching for '''.$search.''' in '''.$database."''n'nSTATUS    |    ROWS CONTAINING    |    TABLE/FIELD    (+ERROR)'n";
$output = $rowHeading;
$summary = '';
foreach($tables as $table) {
    $field_sql = 'SHOW FIELDS FROM '.$table;
    $field_q = mysql_query($field_sql,$MJCONN);
    $field_r = mysql_fetch_assoc($field_q);

    do {
        $field = $column;
        $type = $field_r['Type'];
        switch(true) {
            case stristr(strtolower($type),'char'): $typeOK = true; break;
            case stristr(strtolower($type),'text'): $typeOK = true; break;
            case stristr(strtolower($type),'blob'): $typeOK = true; break;
            case stristr(strtolower($field_r['Key']),'pri'): $typeOK = false; break; // DO NOT REPLACE PRIMARY-KEYS
            default: $typeOK = false; break;
        }
        if($typeOK) { 
            $handle = $table.'_'.$field;
            if($queryType=='replace') {
                $sql[$handle]['sql'] = 'UPDATE '.$table.' SET '.$field.' = REPLACE('.$field.','''.$search.''','''.$replace.''')';
            } else {
                $sql[$handle]['sql'] = 'SELECT * FROM '.$table.' WHERE '.$field.' REGEXP('''.$search.''')';
            }

            $error = false;
            $query = @mysql_query($sql[$handle]['sql'],$MJCONN) or $error = mysql_error();
            $row_count = @mysql_affected_rows() or $row_count = 0;

            $sql[$handle]['result'] = $query;
            $sql[$handle]['affected'] = $row_count;
            $sql[$handle]['error'] = $error;

            $output .= ($query) ? 'OK        ' : '--        ';
            $output .= ($row_count>0) ? '<strong>'.$row_count.'</strong>            ' : '<span style="color:#CCC">'.$row_count.'</span>            ';
            $fieldName = '`'.$table.'`.`'.$field.'`';
            $output .= $fieldName;
            $erTab = str_repeat(' ', (60-strlen($fieldName)) );
            $output .= ($error) ? $erTab.'(ERROR: '.$error.')' : '';
            $output .= "'n";
        }
    }while($field_r = mysql_fetch_assoc($field_q));
}

由于mysql没有基于regex的内置替换函数,所以最好在php中进行替换。表中只需要一个id(用于更新查询)。如果还没有,请将其包括在内。

你可能已经在你的剧本中解释了一些东西(我不太理解),但这个答案只是关于你问题的文本。希望从你的整个专栏中删除一次所有价格会有所帮助(无论在哪里找到)。

连接到数据库后,您需要将代码修改为

$sql = "SELECT id,book FROM yourtable";    
$result = mysql_query($sql);    
if (!$result) 
{
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}
if (mysql_num_rows($result) == 0) 
{
    echo "No rows found, nothing to print so am exiting";
    exit;
}
while ($row = mysql_fetch_assoc($result))
{
    $id = $row['id'];
    $book= $row['book'];
    //$pat='/('$|(US Dollar)) 'd+('.'d+)?/';
    $pat = '/'$|(page)|(US)|(dollar)|('d+)|~|'.|(sterling)/i';
    if(preg_match($pat, $book))
    {
       $book= preg_replace($pat, '', $book);
       $sql = "UPDATE yourtable set book='".$book."' where id='".$id."'";
       mysql_query($sql);
    }
}

编辑更改模式($pat值)以回应您的评论。编辑2:现在添加了"页面",我上次错过了