Please Help With Php Script - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › Please Help With Php Script (1208 Views)
| Please Help With Php Script by anonymoux(op): 12:38am On Sep 16, 2014 |
Please guys, I am working on my site. But have this little problem with my php script. I need to check if string have less than 4 characters. $str = "33322222EEEEE"; if(!preg_match( ??, $str)) {msg = str." has less than 4 numbers/characters" } |
| Re: Please Help With Php Script by GodMode: 4:00am On Sep 16, 2014 |
anonymoux: Please guys, I am working on my site.you can use the strlen() or mb_strlen() function but there are other functions. change your if statement to: echo strlen($str); pls read the php manual for your sake. |
| Re: Please Help With Php Script by adexsimply(m): 5:21am On Sep 16, 2014 |
<?php |
| Re: Please Help With Php Script by anonymoux(op): 10:52am On Sep 16, 2014 |
@adexsimply & @GodMode Thank you guyz so much, but you dont seems to get my problem. I cant use strlen because it count the overall number or characters. I need a code than can be like: if I enter: 11111 = it will say: only one character was entered(because I enter 1 five times. so it will disregard all the other extra times 1 was entered) and if I enter 111122223333 = it will say: it will tell me only 3 characters are entered. (Because I enter 1, 2 and 3 repeated it, it wont cant repetitions). Thank you! |
| Re: Please Help With Php Script by adexsimply(m): 12:34pm On Sep 16, 2014 |
anonymoux: @adexsimply & @GodMode <?php NOTE:count_chars accept 2 parameters, the second parameter is known as "mode". In this case, I used "mode 3" which will return a string with all the different characters used. I then counted the characters using the more familiar "strlen" Capisce? |
| Re: Please Help With Php Script by anonymoux(op): 1:16pm On Sep 16, 2014 |
<?php $a = 'apple'; $b = 'ball'; $duplicates = array_count_values(array_merge(str_split($a), str_split($b))); // Array ( [a] => 2 [p] => 2 [l] => 3 [e] => 1 [b] => 1 ) print_r($duplicates); $totalMatches = 0; foreach($duplicates as $count) { if($count > 1) $totalMatches += $count; } // 7 matches! echo $totalMatches . ' matches!'; ?> I have gotten what I needed. Easy and Perfect!!! |
| Re: Please Help With Php Script by adexsimply(m): 1:25pm On Sep 16, 2014 |
Sorry, can you explain? anonymoux: <?php |
| Re: Please Help With Php Script by anonymoux(op): 2:03pm On Sep 16, 2014 |
<?phpAbove is a more understandable version of what I actually wanted. in this above example. $totalMaches will come out as: [b] Array ( [a] => 2 [p] => 2 [l] => 3 [e] => 1 [b] => 1 ) which means letter a appears twice letter p appears twice letter l appears thrice and so on.... counting duplicates and even non duplicates... ![]() |
| Re: Please Help With Php Script by GodMode: 2:42pm On Sep 16, 2014 |
anonymoux: Above is a more understandable version of what I actually wanted.that's not what you explained OOOoooo. but this is similar to what you explained <?php $data = '1111222233333444455555'; $work = array_unique(str_split($data)); echo count($work); ?> |
| Re: Please Help With Php Script by adexsimply(m): 5:03pm On Sep 16, 2014 |
anonymoux: Above is a more understandable version of what I actually wanted.Okay..But, ![]() <?php |
| Re: Please Help With Php Script by GodMode: 7:44pm On Sep 16, 2014 |
adexsimply: Okay..But,U copied that code from the manual... www.php.net/manual/en/function.count-chars.php |
| Re: Please Help With Php Script by anonymoux(op): 12:15pm On Sep 17, 2014 |
thanks adexsimply: Okay..But, |
| Re: Please Help With Php Script by Nobody: 1:55pm On Sep 17, 2014 |
PHP can be ugly sometimes. |
Sorting Out A Simple Text Dictionary With PHP • Can Obfuscated Php Script Be Decoded? • 2 • 3 • 4
Millions Of Websites Hit By Drupal Hack Attack • What Would Nigerian Like To Have On The Internet In 2015 • How To Use Google As A Proxy ?
??, $str)) {
