2

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.

$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.

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)

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)

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!

16
  • 1
    This code doesn't seem sane to me. The if and else both do the exact same thing. Did you trim parts of the code? Did you maybe remove the part that causes the bug? Commented Oct 22, 2015 at 15:38
  • Sorry, there's tons more variables that go into the array I just deleted them for clarity. I edited my question to reflect the code without the if statement, the bug still happens. Commented Oct 22, 2015 at 15:40
  • Try it without extract(). Commented Oct 22, 2015 at 15:42
  • 1
    Also, why not just strip_tags()? Commented Oct 22, 2015 at 15:42
  • 3
    Re "correct me if I'm wrong": Have you tried it? Have you read the examples in the documentation? Commented Oct 22, 2015 at 15:56

1 Answer 1

1

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. 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. http://php.net/manual/es/function.strip-tags.php

Sign up to request clarification or add additional context in comments.

1 Comment

I can accept my own answer in two days time, will set alarm. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.