strpos 不适用于搜索以 X 开头的术语

strpos doesn't work with searching a term start with X

我需要根据$assetName 更改十进制数字的长度。为此,我像这样使用 strpos:

if (strpos($assetName, 'JPY') || strpos($assetName, 'XAG'))
{
    $decimal = 3;
}
elseif (strpos($assetName, 'OIL') || strpos($assetName, 'XAU'))
{
    $decimal = 2;
}
else
{
    $decimal = 5;
}

它在处理 JPY 和 OIL 时不适用于 XAG 和 XAU。如果我尝试使用 XAG 或 XAU $decimal 变为等于 5。如果我使用 AGU 而不是 XAG,它在 $assetName = XAGUSD 时有效。

我猜一开始是 X 导致了问题,但我找不到解决方案。

你应该通过严格的类型检查来检查 strpos

strpos($assetName, 'JPY') !== false

因为当搜索到的字符串恰好在第 0 个索引处匹配时,它在 if 语句中为 false 但如果使用严格类型比较检查则不是 false。

Note:- Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

Returns FALSE if the needle was not found.