₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,020 members, 8,419,970 topics. Date: Thursday, 04 June 2026 at 08:34 AM

Toggle theme

Please Help With Php Script - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingPlease Help With Php Script (1208 Views)

1 Reply (Go Down)

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(huh??, $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.
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(huh??, $str)) {
msg = str." has less than 4 numbers/characters"
}
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
$str = "33322222EEEEE";
if (strlen($str)<3) {

echo $str." has less than 3 characters";
}
else
{
echo $str. " has more than 3 characters";
}
?>
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

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!
<?php
$str = "111122223333";

echo "\"$str\" Contains ".strlen(count_chars($str,3))." distinct character(s)";
?>

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" cheesy cheesy 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
$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 anonymoux(op): 2:03pm On Sep 16, 2014
<?php
$a = 'appleball';

$duplicates = array_count_values(array_merge(str_split($a)));

// Array ( [a] => 2 [p] => 2 [l] => 3 [e] => 1 => 1 )
print_r($duplicates);

$totalMatches = 0;

foreach($duplicates as $count) {
if($count > 1)
$totalMatches += $count;
}
// 7 matches!
echo $totalMatches . ' matches!';
?>
Above 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... grin
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.
in this above example. $totalMaches will come out as:
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... grin
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.
in this above example. $totalMaches will come out as:
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... grin
Okay..But, tongue tongue tongue
<?php
$str = "111122223333666 ";

foreach (count_chars($str, 1) as $i => $count) {
echo "There are $count occurence(s) of \"".chr($i)."\"in the string<br/>";
}
?>
Re: Please Help With Php Script by GodMode: 7:44pm On Sep 16, 2014
adexsimply: Okay..But, tongue tongue tongue
<?php
$str = "111122223333666 ";

foreach (count_chars($str, 1) as $i => $count) {
echo "There are $count occurence(s) of \"".chr($i)."\"in the string<br/>";
}
?>
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, tongue tongue tongue
<?php
$str = "111122223333666 ";

foreach (count_chars($str, 1) as $i => $count) {
echo "There are $count occurence(s) of \"".chr($i)."\"in the string<br/>";
}
?>
Re: Please Help With Php Script by Nobody: 1:55pm On Sep 17, 2014
PHP can be ugly sometimes.
1 Reply

Sorting Out A Simple Text Dictionary With PHPCan Obfuscated Php Script Be Decoded?234

Millions Of Websites Hit By Drupal Hack AttackWhat Would Nigerian Like To Have On The Internet In 2015How To Use Google As A Proxy ?