I have an associative array that is built dynamically from a MySQL table like so:
array(['p.id'] => 3, ['p.fname'] => 'Bill', ['p.lname'] => 'O\'Reilly')
This is a large array and was built this way for reasons that are too long to go into here. The problem, as you can see is that when we attempt to access the value of [‘p.lname’] we get “O\”
Anyone have any ideas on how to get around this without modifying the way the array is built? I am currently stripping the slashes and internal apostrophes as a work around, but would prefer to leave the apostrophes in place and just strip the slashes.
This is complicated by the fact that the output goes into a form input like so:
$field = "<input type='text' name='$input_unique_id' style='width:$width;' value='$array_value' />";
That’s right so far.
And this is a different issue: You use single quotes for the HTML element attributes, so you cannot use them in the attribute value like that*. Attribute values always should be escaped with
htmlspecialchars(you will have to set theENT_QUOTESflag in this case)*) your current HTML (with
stripslashesapplied) looks like this:The
Reilly'part is invalid and thus ignored, this leavesvalue='O'