简体   繁体   English

对数组中的变量使用 preg_replace 时,该变量不会反映 preg_replace 更改

[英]When using preg_replace on a variable in an array, the variable doesn't reflect preg_replace changes

I can't figure out what in the world is going wrong with my code.我无法弄清楚我的代码到底出了什么问题。

Problem: I'm getting results from a mysql DB, one of the variables returned needs to be run through preg_replace, the preg_replace() works just fine when I echo it out, but when I try to put that variable into the array, it doesn't reflect the preg_replace() changes.问题:我从 mysql 数据库获取结果,返回的变量之一需要通过 preg_replace 运行,当我回显时preg_replace()工作得很好,但是当我尝试将该变量放入数组时,它不反映preg_replace()更改。

$bl = array(
    'skills' => array()
);

if ($result = $db->query($queryStmt)) {
    while ($row = mysqli_fetch_assoc($result)) {
        extract($row);
        $newdesc = preg_replace("/\<[^>]+\)/","",$Desc);
        $sk = array(
            'desc' => $newdesc
        );
        array_push($bl['skills'], $sk);
    }
};

header('Content-type: application/json');
echo json_encode($bl, JSON_NUMERIC_CHECK);
$db = NULL;

So again, if I echo the $newdesc variable before the array code, it displays properly, but when the array is echo'd out at the end of the script, it doesn't.同样,如果我在数组代码之前回显 $newdesc 变量,它会正确显示,但是当在脚本末尾回显数组时,它不会显示。

Edit: Someone requested the echo response, if I echo out $newdesc this string: MP Regeneration 3 Bow skills used at every blow mentality may be eligible for as much as 3 each additional (but does not apply to a range type)编辑:有人要求echo响应,如果我回显 $newdesc 这个字符串:MP Regeneration 3 Bow Skills used at each Blow mentality may be allowed to up to 3 each extra (but not apply to a range type)

simply echos out as this: MP Regeneration 3 Bow skills used at every blow mentality may be eligible for as much as 3 each additional (but does not apply to a range type)简单地呼应如下:MP 再生 3 弓技能在每次打击心态时使用,每增加一次可能有资格获得多达 3 个(但不适用于范围类型)

And the code now reflects this:代码现在反映了这一点:

$bl = array(
    'skills' => array()
);

if ($result = $db->query($queryStmt)) {
    while ($row = mysqli_fetch_assoc($result)) {
        extract($row);
        $newdesc = preg_replace("/\<[^>]+\)/","",$Desc);
        echo $newdesc;
        $sk = array(
            'desc' => $newdesc
        );
        array_push($bl['skills'], $sk);
    }
};

header('Content-type: application/json');
echo json_encode($bl, JSON_NUMERIC_CHECK);
$db = NULL;

Edit again: See answer for solution!再次编辑:请参阅答案以获取解决方案!

Thanks to the wonderful people in the comments, the solution to the problem was simply to use strip_tags() this my friends is a fine case of me being an idiot.感谢评论中的好人,解决问题的方法只是使用strip_tags()我的朋友们是我白痴的好例子。 Code is now this:代码现在是这样的:

$bl = array(
    'skills' => array()
);

if ($result = $db->query($queryStmt)) {
    while ($row = mysqli_fetch_assoc($result)) {
        $newdesc = strip_tags($row['Desc']);
        $sk = array(
            'desc' => $newdesc
        );
        array_push($bl['skills'], $sk);
    }
};

header('Content-type: application/json');
echo json_encode($bl, JSON_NUMERIC_CHECK);
$db = NULL;

And it works perfectly.它完美地工作。 I was making a function that already existed, read the documentation on strip_tags() for more info.我正在制作一个已经存在的函数,请阅读有关strip_tags()的文档以获取更多信息。 http://php.net/manual/es/function.strip-tags.php http://php.net/manual/es/function.strip-tags.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM