字符串/数组检查php


String/Array checking php

如何检查数组的前2个字符是否为0x?这里有一个例子:

$hex = "0xFFFF";
if($hex[0:2].find('0x')==0)
{
print("0x Found.");
}
else
{
print("0x Not Found.");
}

有人能创造一个可行的替代方案吗?

如果$hex是字符串,则相当容易

if (strpos($hex, '0x') === 0) {
    print("0x Found.");
} else {
    print("0x Not Found.");
}

使用strnicmp(手动(看起来不错。

$hex = '0xFFFF';
if (strnicmp($hex, '0x', 2) == 0)
{
    print("0x Found.");
}
else
{
    print("0x Not Found.");
}

$hex变量的开头查找不敏感的"0x"字符串。

$hex = '0xFFFF';
if ($hex[0].$hex[1] == '0x')
{
    print("0x Found.");
}
else
{
    print("0x Not Found.");
}

无需使用任何功能。有关它的用法,请参阅此页。

您可以将字符串字符作为数组访问,以获取第一个和第二个索引,并检查它们是否为0和x。

<?php
$hex = array("0xFFF","5xFFF","0xDDD");
$len = count($hex);
$msg = "";
for ($i = 0; $i < $len; $i++) {
    if ($hex[$i][0] == "0" && $hex[$i][1] == "x") {
        $msg .= $hex[$i] . ' starts with 0x!' . "'n";
    }
}
echo ($msg);
?>