I am using Linux (14.04.2 LTS trusty).
In my PHP script, I output data (varchar) retrieved from several columns within a MySQL table.
The data in some columns contains new lines/paragraphs, spaces, periods, and many other characters. For example, here is one of the values I am working with:
var_dump($value2)
shows string(16) " redness."
For some columns, I enter the following code to remove certain unwanted data before outputting...
$value3 = preg_replace('#[ \,\";\.
]#', '', $value2);
echo $value3;
which outputs: redness
...it works.
Realizing that I use the code above often (along with additional code not presented here), I decided to create a function that includes it and then call the function (which is included from another file) to output the remaining columns.
However, when I call the function, and
does not seem to work and the output displays those characters literally:
redness
Based on answers to related questions in this forum, I tried the following:
$value3 = preg_replace('#[ \\|\\\\ |\\ |\,|\"|;|\.]#', '', $value2);
which outputs: edess
focusing on the and
, I tried
$value3 = preg_replace('#[ ]+#', '', $value2);
which outputs: redness
.
I also tried $value3 = str_replace(array(" ", ""), '', $value2);
which outputs: redness.
I tried $value3 = trim(preg_replace('/\s\s+/', ' ', $value2));
which outputs: redness.
I tried $value3 = preg_replace('/\s+/', '', $value2);
which outputs: redness.
I tried echo preg_replace('/[ ]/','',$value2);
which outputs: redness.
I tried echo preg_replace('/(\)(\ )/','',$value2);
which outputs redness.
Why does preg_replace not remove and/or
if I call it from a function within an included file?
See Comments. I had created an array to store the column names from the database's table...
$Command = "DESCRIBE {$Table}";
$Command = mysqli_query($mysqli, $Command) or die("Error: ".mysqli_error($mysqli));
if($Command)
{
$TableField = array();
while($row = mysqli_fetch_array($Command))
{
$TableField[] = $row['Field'];
}
}
...But the array that stores the values was assigned the incorrect keys:
while($newArray = mysqli_fetch_array($Execute)) foreach($TableField as $key => $value) {$GoodData[$key] = $newArray[$key];}
...The problem disappeared when the array was assigned the correct keys:
while($newArray = mysqli_fetch_array($Execute)) {foreach($TableField as $key => $value) {$GoodData[$value] = $newArray[$value];}