简体   繁体   中英

Count how many times a data-attribute or text string exists in a string and return the number for each one

I have a little PHP & JavaScript app that cURL requests a list of URLs and writes to a log file when any URL in that list of URLs returns a, 404 or 500 status code for example.

Ok what I am trying to do is count with JavaScript how many of each URL exists in the log file and return the number. I want to return a structure kind of like this:

<ul>
    <li>http://www.example.com  (5)</li>
    <li>http://anothersite.org (19)</li>
    <li>http://www.more.com    (16)</li>
</ul>

In the log file I have given each URL entry a data-attribute and looks like this:

<p class="log-text">
    <span data-url="www.examplezz.com">www.examplezz.com - 0 (Timeout)</span> 
    <span data-url="www.examplezz.com/othersite">www.examplezz.com/othersite - 0 (Timeout)</span>
    <span data-url="www.idontexist.io">www.idontexist.io - 0 (Timeout)</span> 
    <span data-url="www.idontexist.io/othersite">www.idontexist.io/othersite - 0 (Timeout)</span> 
</p>
<p class="log-text">
    <span data-url="www.examplezz.com">www.examplezz.com - 0 (Timeout)</span> 
    <span data-url="www.examplezz.com/othersite">www.examplezz.com/othersite - 0 (Timeout)</span>
    <span data-url="www.idontexist.io">www.idontexist.io - 0 (Timeout)</span> 
    <span data-url="www.idontexist.io/othersite">www.idontexist.io/othersite - 0 (Timeout)</span>
</p>
<p class="log-text">
    <span data-url="www.examplezz.com">www.examplezz.com - 0 (Timeout)</span> 
</p>

The log file is fetched via ajax, and I get it as a string. Here is a JS Bin with the "for" loop I have been trying to work with:

JSBin

Additional info: Here is what my ajax request looks like, not sure if it helps.

 var overLoad = function (){ $.ajax({ type: 'POST', url: 'includes/logfiles/'+logfilename+'.php', data: $('.log-text'), statusCode: { 404: function() { $("<p class='intro log-text eventNum' style='margin-bottom:0;'>Is your internet connection intact?</p>").appendTo(".overview-popup"); } } }).done(function (data) { // here I want to count the urls in the 'data' string }).fail(function (jqXHR, statusCode) { $("<p class='intro log-text eventNum' style='margin-bottom:0;'>No offline events!</p>").appendTo(".overview-popup"); }); }; 

Why try to do string manipulation when you have the DOM at your finger tips?

var div = document.createElement('div');
div.innerHTML = data;
var elements = div.querySelectorAll('[data-url]');
var urls = {};

for(var i = 0; i < elements.length; i++) {
    var url = elements[i].getAttribute('data-url');

    if(!(url in urls)) {
        urls[url] = 0;
    }

    urls[url]++;
}

console.log(urls);

http://jsbin.com/adEBiCU/3/edit

I leave the grouping by host name (instead of just url) to you as an exercise. Hint: use the DOM (double hint: <a> has a host property).

Edit: I missed that you're already using jQuery.

var data ='<p class="log-title">not available. <span class="stamp"><span >Pinged: </span>Wednesday 1st of January 2014 03:57:03 PM</span></p><p class="log-text"><span data-url="www.examplezz.com">www.examplezz.com - 0 (Timeout)</span><br /><span data-url="www.examplezz.com/othersite">www.examplezz.com/othersite - 0 (Timeout)</span><br /><span data-url="www.idontexist.io">www.idontexist.io - 0 (Timeout)</span><br /><span data-url="www.idontexist.io/othersite">www.idontexist.io/othersite - 0 (Timeout)</span><br /></p>';

var urls = $(data).find('[data-url]').map(function() {
    var url = $(this).attr('data-url');
    return $('<a>', {href: 'http://' + url})[0].host;
}).get().reduce(function(urls, b) {
        if(!(b in urls)) {
    urls[b] = 0;
    }

    urls[b]++;

    return urls;
}, {});


console.log(urls);

http://jsbin.com/adEBiCU/5/edit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM