Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count occurrences with regex

Status
Not open for further replies.

Geates

Programmer
Aug 25, 2009
1,566
US
Currently, I am using a foreach() loop to count the occurrences of numbers in a string...and to count uppercase letters...and to count lowercase letter...and to count special chars. I feel like there is a "simpler" to do it with regex. Is there?

Code:
$n = 0;
$string_as_array = str_split("12sCdf@5sd78%");
foreach($string_as_array as $char) {
	if (is_numeric($char)) {
		$n += 1;
	}
}

-Geates
 
Hi

I would probably prefer it this way :
PHP:
[COLOR=orange]preg_match_all[/color][teal]([/teal][i][green]'/(?<digit>\d+)|(?<upper>[A-Z]+)|(?<lower>[a-z]+)|(?<special>[@#$%&]+)/'[/green][/i][teal],[/teal] [i][green]'12sCdf@5sd78%'[/green][/i][teal],[/teal] [navy]$match[/navy][teal]);[/teal]

[b]foreach[/b] [teal]([[/teal][i][green]'digit'[/green][/i][teal],[/teal] [i][green]'upper'[/green][/i][teal],[/teal] [i][green]'lower'[/green][/i][teal],[/teal] [i][green]'special'[/green][/i][teal]][/teal] [b]as[/b] [navy]$type[/navy][teal])[/teal]
    [b]echo[/b] [i][green]"found "[/green][/i][teal],[/teal] [COLOR=orange]strlen[/color][teal]([/teal][COLOR=orange]implode[/color][teal]([/teal][navy]$match[/navy][teal][[/teal][navy]$type[/navy][teal]])),[/teal] [i][green]" $type characters\n"[/green][/i][teal];[/teal]

I wrote "probably", because may depend on your goal. To me it looks like kind of password checker, so maybe could become something like this :
PHP:
[b]function[/b] [COLOR=orange]check[/color][teal]([/teal][navy]$password[/navy][teal],[/teal] [navy]$requirement[/navy][teal])[/teal]
[teal]{[/teal]
    [COLOR=orange]preg_match_all[/color][teal]([/teal][i][green]'/(?<digit>\d+)|(?<upper>[A-Z]+)|(?<lower>[a-z]+)|(?<special>[@#$%&]+)/'[/green][/i][teal],[/teal] [navy]$password[/navy][teal],[/teal] [navy]$match[/navy][teal]);[/teal]

    [navy]$error[/navy] [teal]= [];[/teal]
    [b]foreach[/b] [teal]([/teal][navy]$requirement[/navy] [b]as[/b] [navy]$type[/navy] [teal]=>[/teal] [navy]$count[/navy][teal]) {[/teal]
        [navy]$found[/navy] [teal]=[/teal] [COLOR=orange]strlen[/color][teal]([/teal][COLOR=orange]implode[/color][teal]([/teal][navy]$match[/navy][teal][[/teal][navy]$type[/navy][teal]]));[/teal]
        [b]if[/b] [teal]([/teal][navy]$found[/navy] [teal]<[/teal] [navy]$count[/navy][teal])[/teal]
            [navy]$error[/navy][teal][] =[/teal] [i][green]"Insufficient {$type} characters : found only {$found} while {$count} are required"[/green][/i][teal];[/teal]
    [teal]}[/teal]

    [b]return[/b] [navy]$error[/navy][teal];[/teal]
[teal]}[/teal]

[navy]$error_list[/navy] [teal]=[/teal] [COLOR=orange]check[/color][teal]([/teal][i][green]'12sCdf@5sd78%'[/green][/i][teal], [[/teal][i][green]'digit'[/green][/i] [teal]=>[/teal] [purple]3[/purple][teal],[/teal] [i][green]'upper'[/green][/i] [teal]=>[/teal] [purple]2[/purple][teal],[/teal] [i][green]'lower'[/green][/i] [teal]=>[/teal] [purple]2[/purple][teal],[/teal] [i][green]'special'[/green][/i] [teal]=>[/teal] [purple]1[/purple][teal]]);[/teal]

Of course, if the password strength is configurable in your project, you can compose the regular expression dynamically.


Feherke.
feherke.ga
 
Thanks, feherke. I like the cleanliness of you first solution but my complexity is configurable so I'll implement the second solution.

-Geates
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top