Tuesday 15 March 2011

php - Preg matching and counting the resulting match in a short string -


I have a function that calculates the number of items in a string ($ paragraph) and tells me how many results There are letters, that is, spoon and spoon present 7, I can use it to complete the percentage of the string.

I need to reinforce it with preg_match because 10tsp should be calculated as 5.

$ letters = stellon ($ paragraph); $ Items = array ("toss", "tbsp", "tbs"); $ Count = 0; Calculate the number of formatting in foreign currency ($ item $ items) {// parameter $ countitems = substr_count ($ paragraph, $ item); $ Countlength = (strlen ($ item) * $ countitems); $ Count = $ count + $ countlength; } $ Composite = (($ 100 / $ character) * $ count);

I know that something like preg_match ('# [d] + [items] #', $ paragraph) will be something correct?

Sorry for the curve ball edit but there can be a place between numbers and $ items, can an example hold preg_match?

It's not quite clear to me that what you're trying to do with regex, but If you are just trying to match for a particular number-measurement combination, then it may help:

$ count = preg_match_all ('/ \ d + \ s * ( Tbsp | tsp | tbs) / ', $ paragraphs);

This will return the number of times, where a number-measurement combination occurs in the $ paragraph .

EDIT

  $ paragraph = "5tbsp and 10 teaspoons"; $ Charcnt = 0; $ Matches = array (); {Preg_match_all ('/ \ d + \ s * (tbsp | tsp | tbs) /', $ paragraph, $ matches) & gt; {foreach ($ matches [0] $ match) {$ charcnt + = Strlen (match $); }} Printf ("Total number of characters:% d \ n", $ charcnt); 


No comments:

Post a Comment