如何检查数组中包含HTML字符的条目? [英] How do I check an array for an entry that contains HTML characters?
问题描述
我有一个数组,表示以HTML字符串表示的希腊字母:
I have an array representing the Greek alphabet represented as HTML character strings:
$alphabet = ["α", "β", "γ", "δ", ...]
类似的字符串变量存储在MySQL数据库中,并从其中检索(我正在使用 mysqli
).当我从数据库中检索变量 $ db_result
的值时,例如,它的值是& alpha;"
并检查其是否在 $ alphabet
数组使用 in_array()
函数,但是从未找到:
Similar string variables are stored in, and retrieved from, a MySQL database (I'm using mysqli
). When I retrieve a variable $db_result
whose value is, say, "α"
from the DB and check to see if it's in the $alphabet
array using the in_array()
function, however, it's never found:
if( in_array($db_result, $alphabet) ){
print_r("That's Greek to me!");
}
/* Result: Nothing ever prints */
如果我 print_r()
和 $ db_result
和 $ alphabet
都打印相同的字符.也就是说, $ db_result
打印为'α'和 $ alphabet
打印为 Array([0] =>& alpha; [1] =>& beta; [2] =>& gamma; ...)
.
If I print_r()
both $db_result
and $alphabet
, they both print the same characters. That is, $db_result
prints as 'α' and $alphabet
prints as Array ( [0] => α [1] => β [2] => γ ...)
.
在页面源中, $ db_resul
t值显示为实际的希腊字母,而 $ alphabet
数组条目为HTML & alpha;
.我并不关心显示,而是逻辑:我需要 in_array()
来将alpha识别为alpha
In the page source, the $db_resul
t values show up as actual Greek letters, while the $alphabet
array entries are HTML α
. I don't care about the display so much as the logic: I need in_array()
to recognize alpha as alpha
从数据库中检索到值& alpha;"
后,编码方面必须有所不同,但我不知道该如何解决.
Something must differ about the encoding once the value "α"
is retrieved from the DB, but I don't know what or how to fix it.
推荐答案
数据库结果为实际字符,因此将其转换为HTML实体以进行检查:
The database result is the actual character, so convert it to the HTML entity to check:
if( in_array(htmlentities($db_result), $alphabet) ){
print_r("That's Greek to me!");
}
这篇关于如何检查数组中包含HTML字符的条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!