Get the latest IDS

Asked

Viewed 120 times

-1

I want to get the last ID’s of this website

I don’t want to take the IDS with higher numbers, but the last ones in the order: Example:

<effect id="195" lib="CrossTrainer" type="fx" revision="62175"/>
<effect id="500" lib="BigSpn" type="fx" revision="90000"/>
<effect id="501" lib="SmallS" type="fx" revision="90000"/>
<effect id="502" lib="BigJmp" type="fx" revision="90000"/>
<effect id="503" lib="SmallJ" type="fx" revision="90000"/>
<effect id="504" lib="Hoverplan1" type="fx" revision="90000"/>
<effect id="505" lib="Hoverplan2" type="fx" revision="90000"/>
<effect id="506" lib="Hoverplan3" type="fx" revision="90000"/>
<effect id="507" lib="TrampolineTest" type="fx" revision="90000"/>

Catch only the last 3 Ids... "505,506,507"

  • 2

    Could use the str_split() to cut the string into id=" After I cut you into a foreach takes only the first three letters of each item.

  • What the whole code would look like?

  • Is that code really a string? I’ll post the answer.

  • did not understand your question, this code I showed is just an example of the IDS to take from the link: http://hebbohotel.com.br/swf/gordon/RELEASE-HEBBBO/effectmap.xml

2 answers

1


Being an XML you can use DOMDocument

$url = 'http://hebbohotel.com.br/swf/gordon/RELEASE-HEBBO/effectmap.xml';

$xml = file_get_contents($url);

$dom = new DOMDocument;
$dom->loadXML($xml);
$effects = $dom->getElementsByTagName('effect');

foreach ($effects as $effect) {
    echo $effect->getAttribute('id'), PHP_EOL;
}

The previous example served only to understand, to get the last 3 ids use array (convert with iterator_to_array) and use the function array_slice thus:

$url = 'http://hebbohotel.com.br/swf/gordon/RELEASE-HEBBO/effectmap.xml';

$xml = file_get_contents($url);

$dom = new DOMDocument;
$dom->loadXML($xml);
$effects = iterator_to_array($dom->getElementsByTagName('effect'));//Passa para array

//-3 no segundo parametro pega os 3 ultimos itens
$effects = array_slice($effects, -3);

foreach ($effects as $effect) {
    echo $effect->getAttribute('id'), PHP_EOL;
}

Note that file_get_contents fail you may be due to not enabled access to external urls, you can enable this in php.ini (as explained in this reply /a/72746/3635):

allow_url_fopen=1

Or you can trade file_get_contents by Curl, thus:

<?php
$url = 'http://hebbohotel.com.br/swf/gordon/RELEASE-HEBBO/effectmap.xml';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); //Transferência binaria
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Força retornar transferência na variável

$xml = curl_exec($ch);

curl_close($ch);

$dom = new DOMDocument;
$dom->loadXML($xml);
$effects = iterator_to_array($dom->getElementsByTagName('effect'));//Passa para array

Compressed responses GZ

Sometimes it can occur even if you do not request the header Accept-Encoding the server still send, this is a problem perhaps of bad configuration, in case the link you want to access this with this problem, I tried to send the headers, but apparently does not work, so one solution is to use the gzdecode php, so:

  • file_get_contents

    $url = 'http://hebbohotel.com.br/swf/gordon/RELEASE-HEBBO/effectmap.xml';
    
    $xml = gzdecode(file_get_contents($url));
    
    $dom = new DOMDocument;
    $dom->loadXML($xml);
    $effects = iterator_to_array($dom->getElementsByTagName('effect'));//Passa para array
    
  • curl

    $url = 'http://hebbohotel.com.br/swf/gordon/RELEASE-HEBBO/effectmap.xml';
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); //Transferência binaria
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Força retornar transferência na variável
    
    $xml = gzdecode(curl_exec($ch));
    
    curl_close($ch);
    
    $dom = new DOMDocument;
    $dom->loadXML($xml);
    $effects = iterator_to_array($dom->getElementsByTagName('effect'));//Passa para array
    
  • @Rafa I edited the answer, read the ending that add and try, if there is any problem add at the beginning <?php error_reporting(E_ALL); ini_set('display_errors', 1); just to debug, after it’s working you can remove the error_reporting and the ini_set, read more about this at http://answall.com/q/106562/3635

  • @Rafa Voce can not wait for a code ready here on the site because we do not know what you want to do, the rest of your code, the site is not to get code ready and rather treat specific questions, as yours has already been dealt with in the two answers, now if your application of the code in your context is being wrong then it is already difficult to help.

  • @Guilhermenascimento look what he gave http://rafagarcia.eu5.org/efeito.php

  • You think javascript wouldn’t be better?

  • @Rafa They were small problems in the code, probably you used Curl, I fixed them array_values for iterator_to_array and on Curl I add the CURLOPT_RETURNTRANSFER that was missing. Test now the examples.

  • You’re still making mistakes! and it’s all in letters like it’s encrypted http://rafagarcia.eu5.org/efeito.php. if you can contact me via skype would help me more! skype: Rafa.garcia.gamer

  • I used the last issue of the post

  • or Curl, because file_get_contents is all white to the page!

  • @Rafa does the following, creates a new php file called test.php and then pastes this content into it and saves it: http://pastebin.com/raw/q5kMkxRv then accesses it

  • 1

    It worked thanks bro! <3

  • @Rafa I recommend you to study the functions and study php, understand as a comment for you to become better, many of the errors were due to you not knowing the functions you tried to use and not understanding the explanations I went through, it was only in copy+Paste and so failed, is only a constructive criticism :)

Show 6 more comments

0

I don’t know if it’ll help because you’re working with XML, not posted if you’re working with him as string or turning it into array.

I didn’t actually use the str_slipt, did otherwise.

I took a class to get the job done, Getids.php:

<?php
class GetIds
{
    /*
    *   Atributos
    */
    private $string = null;
    private $limit = null;
    private $split = null;

    /*
    *   Construtor com insercao de um atributo
    */
    public function __construct($string = null)
    {
        $this->string = $string;
    }

    /*
    *   Varre atributo string até que o caracter lido não seja string, apos iss une os numeros formando o id
    *   @return result - array com os ids
    */
    public function getIds()
    {
        $this->appLimit();

        $result = array();
        foreach($this->split as $value)
        {
            $isNumber = 0;
            for($letter = 0; $letter < strlen($value); $letter ++)
            {

                if(filter_var($value[$letter], FILTER_VALIDATE_INT) or $value[$letter] === '0')
                    $isNumber ++;
                else
                    break;

            }
            for($letter = 0; $letter < count($value); $letter ++)
            {
                array_push($result, mb_substr($value, 0, $isNumber));
            }
        }

        return $result;
    }

    /*
    *   Muda o valor do atributo limit
    */
    public function setLimit($newLimit)
    {
        $this->limit = $newLimit;
    }

    /*
    *   Separa o atributo string em determinado separador passado por parametro
    */
    public function setSplit($separator)
    {
        if(!is_null($this->string))
            $this->split = explode('id="', $this->string);
        unset($this->split[0]);
    }

    /*
    *   Apaga elementos do array deixando os x ultimos
    */
    private function appLimit()
    {
        if(!is_null($this->string) and !is_null($this->limit))
        {
            $crop = count($this->split) - $this->limit;
            for($item = 0; $item < $crop; $item ++)
            {
                unset($this->split[$item]);
            }
        }

    }
}

Filing cabinet php test.:

<?php
require_once 'GetIds.php';
$myResult =
'<effect id="195" lib="CrossTrainer" type="fx" revision="62175"/>
<effect id="500" lib="BigSpn" type="fx" revision="90000"/>
<effect id="501" lib="SmallS" type="fx" revision="90000"/>
<effect id="502" lib="BigJmp" type="fx" revision="90000"/>
<effect id="503" lib="SmallJ" type="fx" revision="90000"/>
<effect id="5" lib="Hoverplan1" type="fx" revision="90000"/>
<effect id="122150" lib="Hoverplan2" type="fx" revision="90000"/>
<effect id="50654" lib="Hoverplan3" type="fx" revision="90000"/>
<effect id="502170" lib="TrampolineTest" type="fx" revision="90000"/>';

// cria uma classe com a string de resultado
$ids = new GetIds($myResult);

// define o limite para 3
$ids->setLimit(3);

// define onde devo cortar o resultado
$ids->setSplit('id="');

// pega os ids encontrados 
$result = $ids->getIds();

var_dump($result);

Exit:

array (size=3)
  0 => string '122150' (length=6)
  1 => string '50654' (length=5)
  2 => string '502170' (length=6)

Obs: I changed the ids just to show that it works with id of different lengths.

  • 2

    The var_dump displays the array in a more elegant way, the ids are stored on Aray, if you don’t understand this I suggest you study array and more about programming language

Browser other questions tagged

You are not signed in. Login or sign up in order to post.