如何获得正确的数组html彩色索引

here's my simple source code

<?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 


?>

I need to get colored index to my output program. I've tried to add intval, is_int to convert it to integer. But nothing happened. What should i do to get the right colored index of array?

You need a more reliable method of extracting the number than intval(). Intval() will just convert anything that is not an obvious number to "0". I would suggest you try with preg_match() for example. Try experementing with this:

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

Or a simpler solution would be:

echo strip_tags($index2);