php - 使用其他变量创建 PHP 字符串...手动工作,无法弄清楚如何自动工作

标签 php string variables mysql

我正在尝试使用从 mysql 数据库中提取的数据自动形成一个变量。我知道数据是以某种类似于其原始形式的形式从数据库中提取的,但该数据的行为与手动键入并分配给字符串的数据不同。

例如,如果 mysql 表中的一个单元格显示...

I said "goodbye" before I left.
She also said "goodbye."

...我手动复制/粘贴它并添加必要的转义...

$string1 = 
"
I said \"goodbye\" before I left.
She also said \"goodbye.\"
";

...不等于...

$string1 = $mysqlResultArray['specificCellWithQuoteShownAbove']

有趣的是,如果我回显 $string1 的两个版本并查看输出,它们似乎完全相同。但当使用我创建的各种功能时,它们的功能并不相同。仅当我执行手动复制/粘贴方法时,这些功能才起作用 - 显然这不是我想要的。

我不确定这是否与换行符或转义符有关,或者两者的某种组合有关。但是,虽然两个字符串表面上相同,但它们在功能上显然不同,我不知道为什么。

那么,如何在不手动复制/粘贴 mysql 条目中的内容的情况下创建 $string1 ,而是查询数据并将其分配给 $string1 ,使其在功能上与手动复制/粘贴字符串完全相同?

最佳答案

你们的字符串不一样。你认为他们是,但他们不是。以下是一个快速而肮脏的程序,它将运行两个字符串并逐个字符地比较它们,突出显示任何差异。将此代码添加到您的项目中,并将 $first_string$second_string 替换为您认为相同但实际并非如此的两个字符串。

function output_chrs($i,$chr_1, $chr_2)
{
    echo $i . '.' . $chr_1, '|', $chr_2;
    if($chr_1 === $chr_2)
    {
        echo "\n";
    }
    else
    {
        echo " <---- difference detected\n";
    }
    return; 
}

function chr_by_chr($string_1, $string_2, $output_method)
{
    if(strlen($string_2) < strlen($string_1))
    {
        $string_2 = str_pad($string_2, strlen($string_1), ' ');
    }
    for($i=0;$i<strlen($string_1);$i++)
    {
        call_user_func($output_method, $i, $string_1[$i], $string_2[$i]);
    }

    if(strlen($string_2) > strlen($string_1))
    {
        for($i=$i;$i<strlen($string_2);$i++)
        {
            call_user_func($output_method, $i, $string_1[$i], $string_2[$i]);
        }
    }
    return;
}

$first_string  =  'foobazbar';
$second_string =  'fora';
$output_method = 'output_chrs';

ob_start();
$results = chr_by_chr($first_string,$second_string,$output_method); 
echo "\n";
$results = chr_by_chr($first_string,$second_string,'output_ords');  

$results = ob_get_clean();
if(isset($argv))
{
    echo $results;
}
else
{
    echo nl2br($results);
}

此代码将从命令行或浏览器运行。如上所述,它输出

0.f|f
1.o|o
2.o|r <---- difference detected
3.b|a <---- difference detected
4.a|  <---- difference detected
5.z|  <---- difference detected
6.b|  <---- difference detected
7.a|  <---- difference detected
8.r|  <---- difference detected

0.102|102
1.111|111
2.111|114 <---- difference detected
3.98|97 <---- difference detected
4.97|32 <---- difference detected
5.122|32 <---- difference detected
6.98|32 <---- difference detected
7.97|32 <---- difference detected
8.114|32 <---- difference detected

关于php - 使用其他变量创建 PHP 字符串...手动工作,无法弄清楚如何自动工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4568030/

相关文章:

php - WordPress wpdb 未从数据库获取结果

python - 在 python 字典中打印值时的间距

c - 从字符串中删除空格

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

function - 如何从函数中调用变量

php - 为什么插入不起作用

PHP 通知 : A non well formed numeric value encountered in/var/www/zephyr/library/XenForo/Application. php on line 1534

php - 如何在 Laravel 5.8 项目中申请优惠券获得折扣

Java - 启动时出现错误

javascript - 为什么我无法在 javascript 中传递值?