код хорошо прокомментирован
мне нужно оптимизировать его, объединив операции, которые я выполняю для каждого значения
<?php
устанавливает URL для запуска с
$pagenumber = 20;
устанавливает окончание URL в
while ($pagenumber <= 25) {
создать новый ресурс cURL
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.bkesher.com/frum_detail.php?num=$pagenumber");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// grab URL and pass it to the browser
$content = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
// очищает возврат и сохраняет только таблицу для удаления со страницы
$newlines = array("\t","\n","\r"," ","\0","\x0B");
$newcontent = str_replace($newlines, '', $content);
$start = strpos($newcontent,'>Details<');
$end = strpos($newcontent,'</table>',$start);
$table1 = substr($newcontent,$start,$end-$start);
// $table1 = strip_tags($table1);
// проверяет, что таблица заполнена
if (!empty($table1)) {
// получает имя
$start = strpos($table1,'<td');
$end = strpos($table1,'<br />',$start);
$fnames = substr($table1,$start,$end-$start);
$fnames = strip_tags($fnames);
$fnames = preg_replace('/\s\s+/', '', $fnames);
// получает семью
$start = strpos($table1,'<br />');
$end = strpos($table1,'</td>',$start);
$lnames = substr($table1,$start,$end-$start);
$lnames = strip_tags($lnames);
$lnames = preg_replace('/\s\s+/', '', $lnames);
// получает телефон
$start = strpos($table1,'Phone:');
$end = strpos($table1,'</td> </tr> <tr>',$start);
$phone = substr($table1,$start,$end-$start);
$phone = strip_tags($phone);
$phone = str_replace("Phone:", "" ,$phone);
$phone = preg_replace('/\s\s+/', '', $phone);
// получает адрес
$start = strpos($table1,'Address:');
$end = strpos($table1,'</td> </tr> <tr>',$start);
$ad = substr($table1,$start,$end-$start);
$ad = strip_tags($ad);
$ad = str_replace("Address:", "" ,$ad);
$ad = preg_replace('/\s\s+/', '', $ad);
// получает метку
$start = strpos($table1,'Apt:');
$end = strpos($table1,'</td> </tr> <tr>',$start);
$apt = substr($table1,$start,$end-$start);
$apt = strip_tags($apt);
$apt = str_replace("Apt:", "" ,$apt);
$apt = preg_replace('/\s\s+/', '', $apt);
// получает страну
$start = strpos($table1,'Country:');
$end = strpos($table1,'</td> </tr> <tr>',$start);
$country = substr($table1,$start,$end-$start);
$country = strip_tags($country);
$country = str_replace("Country:", "" ,$country);
$country = preg_replace('/\s\s+/', '', $country);
// получает город
$start = strpos($table1,'City:<br /> State/Province:');
$end = strpos($table1,'</td> </tr> <tr>',$start);
$city = substr($table1,$start,$end-$start);
$city = strip_tags($city);
$city = str_replace("City: State/Province:", "" ,$city);
$city = preg_replace('/\s\s+/', '', $city);
// получает почтовый индекс
$start = strpos($table1,'Zip:');
$end = strpos($table1,'</td> </tr> <tr>',$start);
$zip = substr($table1,$start,$end-$start);
$zip = strip_tags($zip);
$zip = str_replace("Zip:", "" ,$zip);
$zip = preg_replace('/\s\s+/', '', $zip);
// получает письмо
$start = strpos($table1,'email:');
$end = strpos($table1,'</td> </tr>',$start);
$email = substr($table1,$start,$end-$start);
$email = strip_tags($email);
$email = str_replace("email:", "" ,$email);
$email = preg_replace('/\s\s+/', '', $email);
// помещает отдельные результаты в переменную строки
$cleancontent = array($pagenumber, $fnames, $lnames, $phone, $ad, $apt, $country, $city, $zip, $email);
// помещает результаты строки в основную переменную
$stack[] = $cleancontent;
// переходит на следующую страницу
$pagenumber++;
}
// переход на следующую страницу, если таблица пуста
else {
$pagenumber++;
}
}
// получает все результаты и печатает их
print "<table>
<tr>
<td>pagenumber</td>
<td>fnames</td>
<td>lnames</td>
<td>phone</td>
<td>ad</td>
<td>apt</td>
<td>country</td>
<td>city</td>
<td>zip</td>
<td>email</td>
</tr>\n";
foreach ($stack as $val) {
print "<tr>\n";
foreach ($val as $no) {
print " <td>$no</td>\n";}
print "</tr>\n";
}
print "</table>";
?>