Php STRU替换don和x27;我不能用十六进制
为什么不使用此代码。我需要将大文本文件中的Php STRU替换don和x27;我不能用十六进制,php,Php,为什么不使用此代码。我需要将大文本文件中的hex转换为int。 我不知道怎么修 //array with hex +9999 length $number1 = ["90.5E1","11.46E2" "81.60E1","0x216","0xffff","8.05E2","33.30E1","0x21C"]; //file with hex +9999 length $file = '90.5E1 0x216 8.05E2 8.05E2'; foreach ($number1 as $val
hex
转换为int
。
我不知道怎么修
//array with hex +9999 length
$number1 = ["90.5E1","11.46E2" "81.60E1","0x216","0xffff","8.05E2","33.30E1","0x21C"];
//file with hex +9999 length
$file = '90.5E1 0x216 8.05E2 8.05E2';
foreach ($number1 as $value) {
$int = (int)$value;
$file = str_replace($value,$int,$file);
}
return $file;
数组中存在分析错误,缺少逗号
//array with hex +9999 length
$number1 = ["90.5E1","11.46E2", "81.60E1","0x216","0xffff","8.05E2","33.30E1","0x21C"];
//file with hex +9999 length
$file = '90.5E1 0x216 8.05E2 8.05E2';
foreach ($number1 as $value) {
$int = (int)$value;
$file = str_replace($value,$int,$file);
}
echo $file;
返回:
905 0 805 805
如果数组中有解析错误,则缺少逗号
//array with hex +9999 length
$number1 = ["90.5E1","11.46E2", "81.60E1","0x216","0xffff","8.05E2","33.30E1","0x21C"];
//file with hex +9999 length
$file = '90.5E1 0x216 8.05E2 8.05E2';
foreach ($number1 as $value) {
$int = (int)$value;
$file = str_replace($value,$int,$file);
}
echo $file;
返回:
9050805805
您可以使用intval(“0xFF”,0)
将类似“0xFF”
的字符串转换为十六进制。0
使其检测0x
前缀并自动从十六进制转换
//array with hex +9999 length
$number1 = ["90.5E1","11.46E2","81.60E1","0x216","0xffff","8.05E2","33.30E1","0x21C"];
//file with hex +9999 length
$file = '90.5E1 0x216 8.05E2 8.05E2';
foreach ($number1 as $value) {
if (substr($value, 0, 2) == '0x')
$int = intval($value, 0);
else
$int = (int)$value;
$file = str_replace($value,$int,$file);
}
echo $file;
输出:905534805805
编辑:
如果要在不使用$number1
的情况下替换值,可以将文件按空格分割并替换每个值:
$file = '90.5E1 0x216 8.05E2 8.05E2';
$file = preg_replace_callback('/([^ ]+)/', function ($matches) {
$value = $matches[0];
if (substr($value, 0, 2) == '0x')
return intval($value, 0);
else
return (int)$value;
}, $file);
echo $file;
输出:
905534805805
您可以使用intval(“0xFF”,0)
将类似“0xFF”
的字符串转换为十六进制。0
使其检测0x
前缀并自动从十六进制转换
//array with hex +9999 length
$number1 = ["90.5E1","11.46E2","81.60E1","0x216","0xffff","8.05E2","33.30E1","0x21C"];
//file with hex +9999 length
$file = '90.5E1 0x216 8.05E2 8.05E2';
foreach ($number1 as $value) {
if (substr($value, 0, 2) == '0x')
$int = intval($value, 0);
else
$int = (int)$value;
$file = str_replace($value,$int,$file);
}
echo $file;
输出:905534805805
编辑:
如果要在不使用$number1
的情况下替换值,可以将文件按空格分割并替换每个值:
$file = '90.5E1 0x216 8.05E2 8.05E2';
$file = preg_replace_callback('/([^ ]+)/', function ($matches) {
$value = $matches[0];
if (substr($value, 0, 2) == '0x')
return intval($value, 0);
else
return (int)$value;
}, $file);
echo $file;
输出:
905 534 805 805
发布代码的结果事实上-我认为替换工作正常,只是你想替换的值不正确。发布代码的结果事实上-我认为替换工作正常,只是你想替换的值不正确。问题不在于“替换”,查找str\u replace($value,'hello\u wrld',$file)时出现问题代码>也许用“预期输入”和“预期输出”更新您的问题。是否要在不使用$number1
的情况下执行替换?问题不是“替换”,问题是查找str\u replace($value,'hello\u wrld',$file)代码>也许用“预期输入”和“预期输出”更新您的问题。是否要在不使用$number1
的情况下执行替换?其示例im已写入,“搜索”有问题,而不是replace或array其示例im已写入,“搜索”有问题,而不是replace或array