У меня есть функция, которая удаляет данные с веб-страницы. Я выбираю теги, где данные должны быть собраны, и я могу получить результаты. Файл function.php выглядит так:
<meta http-equiv="Content-Type" content="text/HTML; charset=utf-8" />
<?php
function LoadCURLPage($url, $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4 Gecko/20030624 Netscape/7.1 (ax)",
$cookie = '', $referer = '', $post_fields = '', $return_transfer = 1,
$follow_location = 1, $ssl = '', $curlopt_header = 0)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if($ssl)
{
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
}
curl_setopt ($ch, CURLOPT_HEADER, $curlopt_header);
if($agent)
{
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
}
if($post_fields)
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);if($referer)
{
curl_setopt($ch, CURLOPT_REFERER, $referer);
}
if($cookie)
{
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
}
$result = curl_exec ($ch);
curl_close ($ch);
return $result;
}
function extract_unit($string, $start, $end)
{
$pos = stripos($string, $start);
$str = substr($string, $pos);
$str_two = substr($str, strlen($start));
$second_pos = stripos($str_two, $end);
$str_three = substr($str_two, 0, $second_pos);
$unit = trim($str_three); // remove whitespaces
return $unit;
}
?>
и process.php выглядит так:
<?php
error_reporting (E_ALL ^ E_NOTICE);
include 'function.php';
// Connect to this url using CURL
$url1 = 'http://www.remixon.com.tr/remixon.xml';// Letâs use cURL to connect to the
$data1 = LoadCURLPage($url1);// Extract information between STRING 1 & STRING 2
$string_one1 = '<SatisFiyati>';
$string_two1 = '</SatisFiyati>';
$info1 = extract_unit($data1, $string_one1, $string_two1);
$info1 = duzenL($info1);
echo $info1;
?>
Этот process.php отображает только извлеченные данные из первых тегов. Но у меня есть 30 одинаковых тегов в этом URL, и мне нужно очистить их все.
Как я могу получить данные между всеми одинаковыми тегами «SatisFiyati» и «/ SatisFiyati» в одном URL?
Вместо обработки необработанного текста используйте DOMDocument загрузить XML с удаленного сайта. Вы можете извлечь все элементы по тэгу аналогично примеру:
<?php
include 'function.php';
// Connect to this url using CURL
$url1 = 'http://www.remixon.com.tr/remixon.xml';
$data1 = LoadCURLPage($url1);
$dom = new DOMDocument;
$dom->loadXML($data1);
$items = $dom->getElementsByTagName('SatisFiyati');
foreach ($items as $item) {
// do something with the data here
echo $item->nodeValue, PHP_EOL;
}
Ты можешь использовать preg_match_all()
который возвращает все совпадения для регулярного выражения.
http://php.net/manual/en/function.preg-match-all.php
В вашем случае ваша функция extract_unit()
будет что-то вроде:
function extract_unit($string, $start, $end)
{
preg_match_all("/" . $start . "([^<]*)" . $end . "/", $string, $matches, PREG_PATTERN_ORDER);
return $matches[1];
}
$matches[0]
содержит массив строк, соответствующих полному шаблону, и $matches[1]
содержит массив строк, заключенных в теги. Так что вам на самом деле нужно $matches[1]
,