简体   繁体   中英

Simple bullet point issue

I am trying to catch bullet points which was given in textfield and trying to replace it with something else, because it is being shown as ? instead of • after rendering the given text to the user. I tested like this: I wrote text with bullet points in word and copy-pasted into textfield.

My vision is this:

$test = strstr($input,'•');
if($test){ echo "bullet point found!";
}

but it is not working or not catching, or • is the wrong regexp to catch the bullet points.

strstr is unlikely to catch when you specify • because the former is a native character, and the latter a HTML entity.

It doesn't matter though: you should fix the underlying issue instead.

You're likely to have an encoding problem that's not limited to bullet points. Seeing a character means that you are feeding a non-UTF-8 character into UTF-8 output.

Reasons for this could be:

  • A source file (where the character is stored) that is saved in the wrong encoding, eg Windows-1252 instead of UTF-8 (check the "Save As..." dialog of your IDE)

  • A database connection that uses latin1 as the connection encoding (even though the tables are UTF-8)

See UTF-8 all the way through for a comprehensive list of things to look at.

Try • instead of • .

See Special Characters in HTML.

It's likely that the HTML isn't passing &bull, but the HTML equivilent.

If that doesn't work to find it, look into HTMLEntities() , which may realistically be the better way to go (unless maybe you're ONLY looking to deal with bullets).

EDIT: Decode the HTML with html_entity_decode() , re-encode to the user with HTMLEntities().

First, before checking the input make sure to encode all characters into its HTML equivalents (which • is). So like this:

$test = strstr(htmlentities($input), '•');

if($test) {
  echo "bullet point found!";
}

However, if you encode your input using htmlentities , all UTF-8 characters should be rendered fine to the user afterwards, as the storage won't need to deal with UTF-8 anymore, thus eliminating artifacts.

Also: Please make sure you are escaping user input when saving it into the database, preferably using equivalents of bound parameters as seen in PDO.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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