[英]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. 我有一个PHP和JavaScript小应用程序,当该URL列表中的任何URL返回例如404或500状态代码时,cURL会请求一个URL列表并写入日志文件。
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. 好的,我想做的是用JavaScript计算日志文件中每个URL的数量并返回数字。 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: 在日志文件中,我为每个URL条目赋予了一个数据属性,如下所示:
<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. 日志文件是通过ajax提取的,我将其作为字符串获取。 Here is a JS Bin with the "for" loop I have been trying to work with:
这是我尝试使用的带有“ for”循环的JS Bin:
Additional info: Here is what my ajax request looks like, not sure if it helps. 附加信息:这是我的ajax请求的外观,不确定是否有帮助。
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? 当您掌握了DOM时,为什么还要尝试进行字符串操作?
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 http://jsbin.com/adEBiCU/3/edit
I leave the grouping by host name (instead of just url) to you as an exercise. 作为练习,我将按主机名(而不只是URL)分组。 Hint: use the DOM (double hint:
<a>
has a host
property). 提示:使用DOM(双重提示:
<a>
具有host
属性)。
Edit: I missed that you're already using jQuery. 编辑:我想念您已经在使用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 http://jsbin.com/adEBiCU/5/edit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.