1

In this project I am sifting through non formatted HTML code and looking for a specific value, below is a section of code from a paragraph deconstruction function. I am using preg_replace to get rid of any unneeded html code until I end up with a small array of variables delimited by the html
tag. Below is the section of logic I am having a problem with. For some reason I am receiving an un-defined offset 5 error when attempting to print the 5th position of the array $pieces. I am using the explode function to create the array and have included the chunk of html code I am running explode on.

$new_broken_var = preg_replace($para_search, $para_replace, $para_subject);
$pieces = explode("<br>", $new_broken_var);
echo $pieces[5];

//upon echoing $new_broke_var I received the below html as it's value           
//<font face="Arial" size="2">For 06/01/13 to 06/30/13<br>
//Report Generated: Thursday, July 11,   2013<br>Unit:   204&nbsp;<br>
//Driver:<br>Owner:<br>Number of Trips: 27<br>Fuel Type:   Diesel</font>


   //creates an array using <br> as it's delimiter on the commented code above
   //the variable we want is in position number 5 within the array. and confirmed via print_f($pieces); 
4

1 回答 1

0

检查请求的索引是否确实存在于您的数组中是一个很好的做法。

if (isset($pieces[5])) {
    echo $pieces[5];
}

由于各种原因,索引可能不存在,但在您的情况下,您的段落似乎是空的,导致explode()返回一个包含单个元素的数组。

于 2013-08-19T06:30:57.193 回答