tags:

views:

76

answers:

3

The webpage has the following source (only copied the important part):

<h1>Relatórios</h1>
<div id="textmenu">
   <a href="berichte.php" class="selected ">Tudo</a>
 | <a href="berichte.php?t=2">Comércio</a>
 | <a href="berichte.php?t=1">Reforços</a>
 | <a href="berichte.php?t=3">Ataques</a>

 | <a href="berichte.php?t=4">Outros</a>
</div>
<form method="post" action="berichte.php" name="msg">
<table cellpadding="1" cellspacing="1" id="overview"
    class="row_table_data">
    <thead>
        <tr>
            <th colspan="2">Assunto:</th>
            <th class="sent">
enviada</th>

        </tr>
    </thead><tfoot>
        <tr>            <th>&nbsp;</th>
            <th class=buttons><input name=del value=apagar              alt=apagar type=image id=btn_delete class=dynamic_img
                src=img/x.gif /></th>
                    <th class=navi>&laquo;<a href="berichte.php?s=10&amp;o=0">&raquo;</a></th>

        </tr>
    </tfoot>
    <tbody>

<tr>
            <td class="sel"><input class="check" type="checkbox" name="n1"
                value="15737030" /></td>
            <td class="sub"><img src="img/x.gif"
                class="iReport iReport1"
                alt="Ganhou como atacante sem perdas"
                title="Ganhou como atacante sem perdas" />
            <div><a href="berichte.php?id=15737030">Rio Grande Do Leste ataca lcsanchez da aldeia</a> </div>
            </td>
            <td class="dat">hoje 21:33</td>
        </tr>
        <tr>

            <td class="sel"><input class="check" type="checkbox" name="n2"
                value="15736877" /></td>
            <td class="sub"><img src="img/x.gif"
                class="iReport iReport1"
                alt="Ganhou como atacante sem perdas"
                title="Ganhou como atacante sem perdas" />
            <div><a href="berichte.php?id=15736877">Rio Grande Do Leste ataca Thabiti da aldeia</a> </div>
            </td>
            <td class="dat">hoje 21:32</td>
        </tr>
        <tr>

            <td class="sel"><input class="check" type="checkbox" name="n3"
                value="15736759" /></td>
            <td class="sub"><img src="img/x.gif"
                class="iReport iReport1"
                alt="Ganhou como atacante sem perdas"
                title="Ganhou como atacante sem perdas" />
            <div><a href="berichte.php?id=15736759">Rio Grande Do Leste ataca Dionisio</a> </div>
            </td>
            <td class="dat">hoje 21:31</td>
        </tr>
        <tr>

            <td class="sel"><input class="check" type="checkbox" name="n4"
                value="15736513" /></td>
            <td class="sub"><img src="img/x.gif"
                class="iReport iReport1"
                alt="Ganhou como atacante sem perdas"
                title="Ganhou como atacante sem perdas" />
            <div><a href="berichte.php?id=15736513">Rio Grande Do Leste ataca Aldeia de troakris</a> (nova)</div>
            </td>
            <td class="dat">hoje 21:30</td>
        </tr>
        <tr>

            <td class="sel"><input class="check" type="checkbox" name="n5"
                value="15736275" /></td>
            <td class="sub"><img src="img/x.gif"
                class="iReport iReport1"
                alt="Ganhou como atacante sem perdas"
                title="Ganhou como atacante sem perdas" />
            <div><a href="berichte.php?id=15736275">Rio Grande Do Leste ataca Thabiti da aldeia</a> </div>
            </td>
            <td class="dat">hoje 21:28</td>
        </tr>
        <tr>

            <td class="sel"><input class="check" type="checkbox" name="n6"
                value="15736220" /></td>
            <td class="sub"><img src="img/x.gif"
                class="iReport iReport1"
                alt="Ganhou como atacante sem perdas"
                title="Ganhou como atacante sem perdas" />
            <div><a href="berichte.php?id=15736220">Rio Grande Do Leste ataca Aldeia de troakris</a> (nova)</div>
            </td>
            <td class="dat">hoje 21:28</td>
        </tr>
        <tr>

            <td class="sel"><input class="check" type="checkbox" name="n7"
                value="15734824" /></td>
            <td class="sub"><img src="img/x.gif"
                class="iReport iReport1"
                alt="Ganhou como atacante sem perdas"
                title="Ganhou como atacante sem perdas" />
            <div><a href="berichte.php?id=15734824">Rio Grande Do Leste ataca Austrália</a> </div>
            </td>
            <td class="dat">hoje 21:18</td>
        </tr>
        <tr>

            <td class="sel"><input class="check" type="checkbox" name="n8"
                value="15734440" /></td>
            <td class="sub"><img src="img/x.gif"
                class="iReport iReport1"
                alt="Ganhou como atacante sem perdas"
                title="Ganhou como atacante sem perdas" />
            <div><a href="berichte.php?id=15734440">Rio Grande Do Leste ataca andrômeda</a> (nova)</div>
            </td>
            <td class="dat">hoje 21:15</td>
        </tr>
        <tr>

            <td class="sel"><input class="check" type="checkbox" name="n9"
                value="15730612" /></td>
            <td class="sub"><img src="img/x.gif"
                class="iReport iReport1"
                alt="Ganhou como atacante sem perdas"
                title="Ganhou como atacante sem perdas" />
            <div><a href="berichte.php?id=15730612">Rio Grande Do Leste ataca wLG02 KING OF POP</a> (nova)</div>
            </td>
            <td class="dat">hoje 20:49</td>
        </tr>
        <tr>

            <td class="sel"><input class="check" type="checkbox" name="n10"
                value="15730304" /></td>
            <td class="sub"><img src="img/x.gif"
                class="iReport iReport1"
                alt="Ganhou como atacante sem perdas"
                title="Ganhou como atacante sem perdas" />
            <div><a href="berichte.php?id=15730304">Rio Grande Do Leste ataca wLG02 KING OF POP</a> (nova)</div>
            </td>
            <td class="dat">hoje 20:47</td>
        </tr>
        </tbody>

</table>
</form>

</div>

I want to read from the source and assign to a variable the values like:

relatorio[0] = berichte.php?id=15730304;

relatorio[1] = berichte.php?id=15730612;

...

relatorio[9] = berichte.php?id=15737030;

How to do that? thanks

A: 

So, this is very rough and based only on what you provided, but:

var inputs = document.getElementsByTagName("input");

var relatorio = new Array();

for(var i = 0; i < inputs.length; i++) {
    var input = inputs[i];
    relatorio[i] = "berichte.php?id=" + input.value;
}

I believe that is what you are going for.

Anthony
what about this one:<input name=del value=apagar alt=apagar type=image id=btn_delete class=dynamic_img src=img/x.gif />won't it mess with the array?
Fernando SBS
I have never heard of a input with the type of "image", and you can definitely improve on my example to be more specific to your code. I would personally just use jquery and avoid the whole mess.
Anthony
A: 

using XPath or just a regex, I will update with some code in a moment. this is regex version, not depending in which element your href is.

function getUrlArray(html) 
{
    relatorio = [];
    var myregexp = /href="(berichte\.php\?id=[\d]+)/g;
    var match = myregexp.exec(html);
    while (match != null) {
        relatorio.push(match[1]);
        match = myregexp.exec(html);
    }
    return relatorio;
}

UPDATE

You can use xhttprequest object to retrieve your page, here is sample

if (window.XMLHttpRequest)     
{ 
    xhr = new XMLHttpRequest();     // Firefox
} 
else if (window.ActiveXObject)   // ActiveX
{
    xhr = new ActiveXObject("Microsoft.XMLHTTP");  // Internet Explorer 
} 

xhr.onreadystatechange = function() 
{
    if (xhr.readyState == 4 && xhr.status == 200)
    {
         var myUrlArr = getUrlArray(xhr.responseText); 
         // At this point you have your array.
    }
}

xhr.open('GET', 'http://yourserver/page.html', true);                  
xhr.send(null); 

I haven't tried but theoretically this should do the work.

Details about Regex:

This is Regular expressions used for matching in a string. It's a small "language" where you describe the pattern and Regular Expression engine will try to match a text using that pattern. It can be used then in many programming languages (C/C++, PHP, Perl, Javascript) or text editing tools.

In this simple regex I said

Find anything that matches href=berichte\.php\?id= (slashes are to escape . and ?, because they are control symbols for regex) literally/as plain text/ then following [\d] (this is character class in square brackets [] and inside \d it says match a digit). Finally a + means one or many times and applies to last group, which is digit. the whole thing is enclosed in () which is capturing group. But you can also avoid it, by just replacing match[1] to match[0]. 0 element will always contain the whole match and the others are pointing to capturing groups if they are exist. You can have many capturing groups. It could have been useful if you wanted to capture only the id value, then you could just write like that

/href="berichte\.php\?id=([\d]+)/g

this will fill up match1 with id values. Sorry can't explain you all regex here, but there are plenty of resources in google.

Time

var myregexp = /(berichte\.php\?id=[\d]+).+?<td class="dat">(.+?)</td>/g;

Something like this. match[2] will contain pattern matching (.+?) which means anything with quantity one or more, lazy matching and located between <td class="dat"> and </td> literally.

Michael
I will try that as soon as I can. thank you
Fernando SBS
ok, now how do I pass the URL address to that code you made? I mean, the URL to the source where it will find the array value. The address is http://s4.travian.com.br/berichte.php, but if it can "find" the URL searching for berichte.php it would be better.
Fernando SBS
is it the (html) where I should put the URL address of the on search document? I tried that without success.
Fernando SBS
@Fernando SBS: the `html` is a string containing your page. I've added some code which will give you idea how to retrieve page.
Michael
can you please explain /href="(berichte\.php\?id=[\d]+)/g; this to me? what are those codes you've used?
Fernando SBS
@Fernando SBS: updated
Michael
humm I see, thank you very much, I understand now =) do you have msn michael?
Fernando SBS
A: 

Michael, now if I want to also add to the relatorios variable array another info for each variable:

<td class="sel"><input class="check" type="checkbox" name="n1"
                value="15734440" /></td>
            <td class="sub"><img src="img/x.gif"
                class="iReport iReport1"
                alt="Ganhou como atacante sem perdas"
                title="Ganhou como atacante sem perdas" />
            <div><a href="berichte.php?id=15734440">Rio Grande Do Leste ataca andrômeda</a> </div>
            </td>
            <td class="dat">hoje 21:15</td>

the interested part would be the time, from each <td class="dat">hoje 21:15</td>, how would your code change to also do that?

Fernando SBS
@Fernando SBS: I've updated my original post. However, as you deviate more from your original question, you should consider accepting one of the answers here and open new question. Sorry, I have icq, but prefer to answer in public so others can also benefit.
Michael
what's your ICQ michael?
Fernando SBS