Reputation: 219
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!
Upvotes: 2
Views: 81
Reputation: 219
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
Upvotes: 1