Solved

count number of results in a string search

Posted on 2012-04-13
7
170 Views
Last Modified: 2012-06-21
Hello,
I have this string and I need to check how many times '<li>' is present please:

<li>aho</li><li>hao</li><li>loa</li><li>ola</li>

Open in new window


thank you
0
Comment
Question by:eMisc
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 7

Expert Comment

by:karunamoorthy
ID: 37843831
which long u r using, php or ....
0
 

Author Comment

by:eMisc
ID: 37843863
Experts Exchange- Scripting Languages- JavaScript- Count Number Of Results In A String Search
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 37843867
Assuming the <li>s are inside a <ul>, then in javascript you could do this:
<body>
<ul id="ul1">
<li>aho</li><li>hao</li><li>loa</li><li>ola</li>
</ul>
<script type="text/javascript">
alert(document.getElementById("ul1").getElementsByTagName("li").length);
</script>
</body>

Open in new window

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 8

Assisted Solution

by:JT92677
JT92677 earned 50 total points
ID: 37843885
If you get the lengths of the substring you're looking for, and the length of the string being analyzed, then
use a Replace command, such as str_replace all occurrances of the substring with '' (nothing) in the analyzed string.

Then the number of occurrances woud be  

( (length of string before replacement) - (length of string after replacement) ) / (Length of target substring)

Kind of a generic solution if you have a function to replace a substring within a string, or write such a function
if whatever language you're using doesn't have it.  String replacements can be done in several ways, but
once the string has been emptied of the substring, the lengths before and after will tell you how many
substrings existed originally.

There might be simpler solutions, but this would work.
0
 
LVL 38

Accepted Solution

by:
Tom Beck earned 250 total points
ID: 37843900
How about this?
<script type="text/javascript">
var liCount = "<li>aho</li><li>hao</li><li>loa</li><li>ola</li>".split("<li>").length - 1;
alert(liCount);
</script>

Open in new window

0
 

Author Closing Comment

by:eMisc
ID: 37843938
thank you!
0
 
LVL 8

Expert Comment

by:JT92677
ID: 37844278
eMisc -- tommyboy figured you were doing this in javascript. Okay, if so there are some useful properties and methods in javascript that you might want to check out as alternative ways to deal with strings and javascript.

For example:

    mystr = "<li>aho</li><li>hao</li><li>loa</li><li>ola</li>";

You can get the length as    lenmystr = mystr.length;

Then use replace method with the global match switch to replace all occurrences in the string

    mystr.replace("<li>"/gm, "");   to replace globally all the "<li>" found with nothing.

Now  mystr.length; property will be the length of the string without all the "<li>" substrings.

and the number of matches = (lenmystr - mystr.length) / "<li>".length

Check out http://www.w3schools.com/jsref/jsref_obj_string.asp

The /g switch means look for all (global), the /m means multi-line.

Just FYI - I realize the question is closed, but someone else may benefit, who knows.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now