【问题标题】:MySQL Query Into Array for preg_replaceMySQL 查询到 preg_replace 的数组
【发布时间】:2015-02-21 10:33:46
【问题描述】:

原始代码

$sentance="are you hungry too?";
function newLanguage($text) {
	$sql = "SELECT in,out FROM words";
	$res = mysql_query($sql) or die();
	$in_array = array();
	$out_array = array();
	while ($row = mysql_fetch_array($res)){ 
		$in_array[] = $row['in']; // table in, NEW words
		$out_array[] = $row['out']; //table out, ENG words
	}
	return preg_replace($in_array,$out_array,$text);
}
$newwords = newLanguage($sentance);
echo $newwords;

修改后的代码:

ini_set("display_errors", "1"); 
error_reporting(E_ALL);

function newLanguage($text) {

$sql = "SELECT in,out FROM words";
$res = mysql_query($sql) or die();
$in_array = array();
$out_array = array();
while ($row = mysql_fetch_array($res)){ 
	$in_array[] = '/\b' . preg_quote($row['in']) . '\b/'; // table in, NEW words
	$out_array[] = $row['out']; //table out, ENG words
}

/* VERSION 2 - STATIC, FOR DEBUGGING
$in_array = array('~\you~s','~\to~s','~\too~s');
$out_array = array('noa','nie','niee');*/

return preg_replace($in_array,$out_array,$text);
}
$sentance="are you hungry too?";
$newwords = newLanguage($sentance);

var_dump($in_array);
echo $sentance;

当前代码

ini_set("display_errors", "1"); 
error_reporting(E_ALL);

$sentance="are you hungry too?";
function newLanguage($text) {

$sql = "SELECT * FROM words";
$res = mysql_query($sql) or die();		
while ($row = mysql_fetch_array($res)){ 
    $in_array[] = '/\b' . preg_quote($row['in']) . '\b/'; // table in, NEW words
    $out_array[] = $row['out']; //table out, ENG words
}
return preg_replace($in_array,$out_array,$text);
}
$newwords = newLanguage($sentance);
var_dump($in_array);
echo $newwords;

我的代码有问题,在我的一生中我无法让它工作,我在使用单词过滤器之前曾与 preg_replace 合作过。尽管使用查询结果创建动态数组完全让我失望。我看过一些教程,但没有一个能真正帮助我理解我哪里出错了。

任何帮助将不胜感激:)

目标: 创建新的语言翻译。数据库包含一行“in”和“out”,既是新语言又是本地语言。

问题: 我不确定我的数组是否已成功填充,因为我的 preg_replace 不起作用。

----更新----

这是我的数据库的样子;

id          in          out
1           you         noa
2           to          nie
3           too         niee
  • 它们存储为 VARCHARS

【问题讨论】:

  • 你的 in 值真的是正则表达式,还是只是普通的字符串?
  • 您可以发布words 表中的示例值吗?确保您启用了error_reporting(E_ALL)?您可能会收到来自 preg_replace 的错误,因为正则表达式上没有正确的分隔符。
  • 如何启用 error_reporting?我以前没有遇到过这种情况。我只是插入吗? ini_set("display_errors", "1");错误报告(E_ALL);在我的查询之上?
  • 是的,你就是这样做的。
  • 好的,就是这样。尽管我不确定如何解决问题,但我没有收到任何错误,但仍然没有什么新的报告。当我删除 $newwords = newLanguage($sentance);我能够得到回声。虽然 $newwords = newLanguage($sentance);在代码中它甚至不会回显句子..有些不对劲。

标签: php mysql arrays replace


【解决方案1】:

BIG 非常感谢 Barmar 的热心帮助和耐心帮助我解决了我的问题! 我已经让他的回答正确,因为他帮助了我这么多,但如果你想使用我评论的最终代码,请随意。

我的目标是导入数据库行并将它们插入到将与 preg_replace 一起使用的数组中。

我的最终代码是:

//this is the base text that will be modified using database information
$sentance="are you hungry too?";

function newLanguage($text) {
    $sql = "SELECT * FROM words";
    $res = mysql_query($sql) or die();	
    $in_array = array();
    $out_array = array();
    while ($row = mysql_fetch_array($res)){
        // inserts the column row 'in' into an array called in_array
        $in_array[] = '/\b' . preg_quote($row['in']) . '\b/';
        // inserts the column row 'out' into an array called out_array
        $out_array[] = $row['out']; 
	}
    //replaced any matching words from 'sentance' with 'in_array' and replaces with 'out_array'
    return preg_replace($in_array,$out_array,$text);
}

//this makes a new variable and used a function to replace the text with matches in the variable
$newwords = newLanguage($sentance); 
echo $newwords;

【讨论】:

  • 您仍然缺少数组的初始化。
  • 它有效,我没有错误。我添加了初始化,但我有错误。谢谢你的帮助:)我真的很感激!
  • 这完全没有意义。您之前收到有关未初始化变量的通知,因为您错过了初始化。
  • 我添加它是由于上面的答案,因为你提到它应该在那里,但我的代码中没有它并且它有效:) PS 谢谢你很有耐心,帮助别人并不容易,它既费时又令人沮丧,但你真的让我很开心:) 谢谢!
  • 初始化应该在循环之前,而不是在循环内部。你完全一无所知。
【解决方案2】:

试试这个:

while ($row = mysql_fetch_array($res)){ 
    $in_array[] = '/\b' . preg_quote($row['in']) . '\b/'; // table in, NEW words
    $out_array[] = $row['out']; //table out, ENG words
}

这会将in 单词转换为正则表达式,添加\b 以匹配单词边界。

我的整个测试代码是:

<?php
$sentance="are you hungry too?";
function newLanguage($text) {
    $in_array = array();
    $out_array = array();
    $rows = array(array('in' => 'you', 'out' => 'noa'),
                  array('in' => 'to', 'out' => 'nie'),
                  array('in' => 'too', 'out' => 'niee'));
    foreach ($rows as $row) {
        $in_array[] = '/\b' . preg_quote($row['in']) . '\b/'; // table in, NEW words
        $out_array[] = $row['out']; //table out, ENG words
    }
    return preg_replace($in_array,$out_array,$text);
}
$newwords = newLanguage($sentance);
echo $newwords;

$rows 数组替换了数据库查询,但其余部分基本相同。

【讨论】:

  • 已更新,仍然无法正常工作。我现在感觉很愚蠢:(并且没有动力。很抱歉浪费了您的时间,感谢您抽出时间提供帮助。
  • 请在您的问题中添加var_dump($in_array)
  • 你要把var_dump的输出加我吗?这就是我想看到的。
  • 如何查看 var_dump 的输出?我在使用 chrome 的控制台中看不到它,我怎么看?我没有看到错误,var_dump 也没有回显任何输出。这肯定意味着我的查询不起作用或数组没有被构建?
  • 这太累了,好像你只是在剪切和粘贴,不知道什么意思。
猜你喜欢
  • 2017-09-15
  • 1970-01-01
  • 2012-07-16
  • 2013-10-06
  • 1970-01-01
  • 1970-01-01
  • 2012-02-16
  • 2017-04-23
  • 1970-01-01
相关资源
最近更新 更多