Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,624 members, 7,809,308 topics. Date: Friday, 26 April 2024 at 07:38 AM

Please Help With Php Script - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Please Help With Php Script (1121 Views)

Sorting Out A Simple Text Dictionary With PHP / Can Obfuscated Php Script Be Decoded? (2) (3) (4)

(1) (Reply) (Go Down)

Please Help With Php Script by anonymoux: 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.
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"
}

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: 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: 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: 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: 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)

After The Basic Of Python / Suggest A Short Code Service Operator For My APP / Help: Please, Recommend A Tool/platform To Build A Social Network Portal

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 23
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.