Best way to clean MySQL string data to plaintext and trim beginning/end spaces?

亡梦爱人 提交于 2019-12-23 04:47:26

问题


I already have data I scraped from a web table and I've noticed some leading spaces and nbsp in some entries. I realize I should have cleaned the data while scraping before inserting it into MySQL but it was a while ago and I don't feel like repeating the process if I don't have to. I came up with this PHP script (late) last night and it works up until I try to update the entries.

<?php
require_once("login.php");

$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());

mysql_select_db($db_database);

$query = "SELECT * FROM ingredients;";

$result = mysql_query($query);
$i = 1;
$e = array();
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    //echo $row[1];
    $str = trim(mysql_real_escape_string($row[1]));
    $e[] = $str;
    $i++;

}

//print_r($e);
/*
$i = 1;
foreach($e as $entry) {
    $query = "UPDATE ingredients
        SET ing_name = $entry
        WHERE ing_id = $i;";
    mysql_query($query);
    $i++;
}*/
?>

Couple of questions:

  1. Is there a way to do this strictly in MySQL without using PHP?
  2. What function/s should I use to strip the strings in order to convert to plaintext and remove all leading, trailing, and nbsp spaces?
  3. The PHP script seems to work until updating the data, what gives?

Thanks


回答1:


There's a large number of MySQL String Functions that can help with this sort of thing.

You might need a combination of REPLACE and TRIM to clean it up:

UPDATE table SET column=TRIM(REPLACE(column, '&nbsp;', ' '))

Also try not to use mysql_query, but use PDO instead. Writing queries directly leaves you exposed to SQL injection problems.




回答2:


I think this will do it for you strictly with a SQL query:

UPDATE ingredients SET column_with_spaces = TRIM(column_with_spaces)

As far as future scraping goes, use trim() before inserting into the database.




回答3:


If you are looking for a mysql solution for string trimming and replacing you should have a look at the mysql string functions found here:

Trim: http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_trim Replace: http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_replace




回答4:


//The PHP keyword <b>strtr()</b> is much easier to understand: <br>
$query = "SELECT * FROM ingredients";<br>
$query= strtr($isql, array('&nbsp~;' => ' ')) ; //without the ~<br>
//Converts all HTML spaces back to white spaces for MySQL to understand.<br>
//Useful when you load data onto textboxes:<br>
//Let's say you have a surname Van Der Merwe with white spaces.<br>
//To edit the surname we need to put it into an input textbox:<br>
 $DBConnect = new mysqli("localhost", "root","Userpassword", "DBName");<br>

For associative array:

if ($result = mysqli_query($DBConnect, $query)) {<br>
  while ($row = mysqli_fetch_assoc($result)) {<br><br>
echo "<>input type='text' name='Surname' size='45'  value=";<br>
echo <b>strtr</b>($row['Surname'], array(' ' => '&nbs#p;')) ;//remove the # "<br>
echo ">";<br><br>

For non-associative array:

if ($result = $DBConnect->query($SQLstring)) {<br>
    while ($row = $result->fetch_row()) {<br>
echo "<dd><>input type='text' name='Surname' size='45'  value=";<br>
echo <b>strtr</b>($row[2], array(' ' => '&nbsp#;')) ; //remove the #<br>
echo ">";<br>


来源:https://stackoverflow.com/questions/10193297/best-way-to-clean-mysql-string-data-to-plaintext-and-trim-beginning-end-spaces

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!