php+替换查询结果数组中的文本

php+替换查询结果数组中的文本,php,mysql,loops,while-loop,Php,Mysql,Loops,While Loop,这是我的方法,无论如何都没有成功,但它可能会让您了解我的代码和编码技能,以及我试图实现的目标 另外,我的方法是不正确的,因为我想做的是设置一组搜索和替换案例。 类似于搜索X并替换Y的列表,而不是仅一个替换。最初我想做一些事情,比如用A替换B,将数组重命名为STEP2,然后用STEP2搜索数组,用C替换为D,然后将数组重命名为STEP3,但是……我认为这可能不是最好的方法=/ 如有任何建议、指示、更正,将不胜感激!提前谢谢 您可以使用PHP中的函数传入替换规则的键=>值对: $data = mys

这是我的方法,无论如何都没有成功,但它可能会让您了解我的代码和编码技能,以及我试图实现的目标

另外,我的方法是不正确的,因为我想做的是设置一组搜索和替换案例。 类似于搜索X并替换Y的列表,而不是仅一个替换。最初我想做一些事情,比如用A替换B,将数组重命名为STEP2,然后用STEP2搜索数组,用C替换为D,然后将数组重命名为STEP3,但是……我认为这可能不是最好的方法=/

如有任何建议、指示、更正,将不胜感激!提前谢谢

您可以使用PHP中的函数传入替换规则的键=>值对:

$data = mysql_query('SELECT * FROM todolist WHERE date > CURDATE()') or die(mysql_error());

// Array of replacement rules. Keys of the array are strings to be replaced.
// Corresponding values of the keys are the replacement strings
$trans = array(
    'stringtoreplace1' => 'replacedstring1',
    'stringtoreplace2' => 'replacedstring2',
    'stringtoreplace3' => 'replacedstring3'
);

while($info = mysql_fetch_array($data)) 
{
    print strtr($info['thingtobedone'], $trans) . ' with ' . $info['persontomeet'] . '<br />';
}
假设thingtobedone是要在其上进行替换的列


然后,当您调用strtr$info['thingtobedone'],$trans时,函数将获取字符串并用相应的值替换$trans数组中每个键值的所有出现。一次替换多个字符串是一种很好的方法。

您希望对SELECT语句中的哪一列执行替换?mysql\u查询的返回值是一种资源,而不是您要搜索和替换的数据这很好!正是我需要的。此外,我们也非常感谢您提供的解释。我试试看,告诉你进展如何!再次感谢!!
$data = mysql_query('SELECT * FROM todolist WHERE date > CURDATE()') or die(mysql_error());

// Array of replacement rules. Keys of the array are strings to be replaced.
// Corresponding values of the keys are the replacement strings
$trans = array(
    'stringtoreplace1' => 'replacedstring1',
    'stringtoreplace2' => 'replacedstring2',
    'stringtoreplace3' => 'replacedstring3'
);

while($info = mysql_fetch_array($data)) 
{
    print strtr($info['thingtobedone'], $trans) . ' with ' . $info['persontomeet'] . '<br />';
}