3

私は文を持っています:

「mysql データベースからテキスト内の単語を見つけて置き換える方法は?」

また、MySQL テーブルの単語は、3 つの列 ID、単語、replaceWord で構成されます。データベースには 4000 語以上あります。

テーブル:

id     word     replaceWord
 1     text     sentence
 2     word     letter
 3     mysql    MySQL
 4     ..       ...
 5     ..       ...
 6     ..       ...

結果:

「 MySQLデータベースから文中の文字を検索して置換する方法は?」

データベースなしでこれを行う方法は知っていますが、データベースが必要です。

 <?php
$text="How to find and replace word in text from mysql database?";
$replaceWord=array(  "text" => "sentence", "word" => "letter", "mysql" => "MySQL");
echo strtr($tekst, $replaceWord);
?>
4

7 に答える 7

3
update YourTable, Words
    set YourTable.YourColumn = replace(YourTable.YourColumn, Words.word, Words.replaceWord)
于 2011-01-28T14:32:43.027 に答える
1
//load all replacements
$result = mysql_query("SELECT * FROM YourTableNameHere");
//replace all words
$words = array();
$replacewords =array();
while ($row = mysql_fetch_assoc($result)) {
    $words[] = $row['word'];
    $replacewords[] = $row['replaceword'];
}
$text = str_replace($words,$replacewords);

preg_replace も必要な場合: 列 isPattern をテーブルに追加する必要があります。

//load all replacements
$result = mysql_query("SELECT * FROM YourTableNameHere");
//replace all words
$words = array();
$replacewords = array();
$preg_words = array();
$preg_replacewords = array();
while ($row = mysql_fetch_assoc($result)) {
    if(!$row['isPattern']){        
        $words[] = $row['word'];
        $replacewords[] = $row['replaceword'];
    }
    else{
        $preg_words[] = $row['word'];
        $preg_replacewords[] = $row['replaceword'];
    }
}
$text = str_replace($words,$replacewords);
$text = $preg_replace($preg_words,$preg_replacewords);
于 2011-01-28T14:35:34.807 に答える
0
select REPLACE(fieldOrString, SearchString, ReplaceString) from table
于 2011-01-28T14:30:22.340 に答える
0

これは、スケーラビリティの点でひどいアイデアです。文を単語ごとに反復し、それぞれで単語が「単語」のどこに存在するかを照会することができます。存在する (データを返す) 場合は、'replaceWord' の内容で置き換えます。

編集:完全にデータベースベースではありませんが、現在のバージョンよりもそうです。

于 2011-01-28T14:30:26.553 に答える
0

$tablesarray 検索する列配列を含める 任意の単語または文字

$PDO データベース接続

    $tablesarray['tablename1']= array('column1' ,'column2' );
    $tablesarray['tablename2']= array('column1' ,'column2' );
    
    $wordstoreplace = ['searchedwordOrLetter1'=>'replacedwordOrLetter1' , 'searchedwordOrLetter2'=>'replacedwordOrLetter2' ]
    foreach($repairkeyarray as $table => $columns){
        foreach($columns as $column){
            foreach($wordstoreplace as $searched => $replaced ){
                $PDO->query("update $table set `$column` = replace(`$column`, '$searched', '$replaced') WHERE `$column` LIKE '%$searched%';");
                @$PDO->execute();
            }
        }
    }
于 2020-08-25T08:16:15.257 に答える
0

@SteveChambersの回答に基づいて独自のクエリの実装を開始した後、置換が結果行ごとに 1 回だけ発生することがわかりました。たとえば、OPで言及されているテーブル レコードが与えられた場合、次のクエリは次のようになります。

SELECT 
    word,
    replaceWord,
    REGEXP_REPLACE(
        'How to find and replace word in text from mysql database?', 
        CONCAT('(?i)(^|\\W)', word, '(\\W|$)'),
        CONCAT('\\1', replaceWord, '\\2')
    ) AS replaced_text 
FROM 
    words 
WHERE 
    'How to find and replace word in text from mysql database?' REGEXP CONCAT('(?i)(^|\\W)', word, '(\\W|$)');

異なる 3 行の結果が返されます。

word  | replaceWord | replaced_text
------+-------------+--------------
text  | sentence    | How to find and replace letter in text from mysql database?
word  | letter      | How to find and replace word in sentence from mysql database?
mysql | MySQL       | How to find and replace word in text from MySQL database?

各行で 1 つの単語のみが置換されていることに注意してください。

いくつかの議論の後、再帰が必要であるという結論に達しました。次のクエリを使用して、プロシージャまたは関数なしでそれを達成することができました。

SELECT 
    (@i := @i + 1) AS i, 
    @tmp := IFNULL(
        REGEXP_REPLACE(
            @tmp, 
            CONCAT('(?i)(^|\\W)', word, '(\\W|$)'), 
            CONCAT('\\1', replaceWord, '\\2')
        ),
        @tmp
    ) AS replaced_text 
FROM 
    (
        SELECT 
            @tmp := 'How to find and replace word in text from mysql database?',
            @i := 0
    ) x
LEFT JOIN 
    words 
ON 
    @tmp REGEXP CONCAT('(?i)(^|\\W)', word, '(\\W|$)') 
ORDER BY i DESC 
LIMIT 1;

このクエリは、元の文字列の各単語を再帰的に置換し、置換を蓄積します。インデックス ( @i) を使用して行に番号を付けます。最後に、すべての累積置換を含む最後の結果 (より大きなインデックス) のみを返します。

LEFT JOINとの組み合わせを使用して、IFNULL置換が行われない場合に元の文字列を返します。

于 2019-11-01T18:07:47.800 に答える