0

这是我的简单源代码

<?php

$index1 = 1;

$index2 = "<span style='color:red'>".$index1."</span>";
$index3 = intval("<span style='color:red'>".$index1."</span>");

$array = array(0=>"Apple", 1=>"Orange");

print_r($array[$index1]);//output will be "Orange"
print_r($array[$index3]);//output "Apple", this should be "Orange"
print_r($array[$index2]);//Notice: Undefined index: 1 


?>

我需要为我的输出程序获取彩色索引。我尝试添加intval,is_int将其转换为整数。但是什么也没发生。我应该怎么做才能获得正确的数组索引?

4

1 回答 1

1

您需要一种比 intval() 更可靠的方法来提取数字。Intval() 只会将任何不明显的数字转换为“0”。例如,我建议您尝试使用 preg_match() 。试试这个:

preg_match('|>([0-9]+)<|', $index2, $matches);
print_r($matches);

或者更简单的解决方案是:

echo strip_tags($index2);
于 2013-08-21T07:56:15.083 回答