Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25745

Check if whole string contains same character

I want to check if a string contains a character repeated zero or more times, for example:

In other words, if the string has 2 or more unique characters, it must return false.

How to go about this in PHP?

PS: Is there a way to do it without RegEx?

Upvotes: 6

Views: 6644

Answers (7)

Sahil Gulati
Sahil Gulati

Reputation: 15141

Regex: ^(.)\1{1,}

^: Starting of string

(.): Match and capture single characted.

\1{1,}: using captured character one or more than once.

For this you can use regex

OR:

PHP code demo

$string="bbbb";
if($length=strlen($string))
{
    substr_count($string,$string[0]);
    if($length==substr_count($string,$string[0]))
    {
        echo "Do something";
    }
}

Upvotes: 0

mickmackusa
mickmackusa

Reputation: 47874

You can check that the number of unique characters is greater than 1. This will perform well even if the input string is empty: (Demo)

$string = 'aaaba';
var_export(
    strlen(count_chars($string, 3)) < 2   // false
);

Alternatively, you can trim the string by its first character, but this will generate warnings/notices if the input string has no length. (Demo)

$string = 'aaaba';
var_export(
    !strlen(trim($string, $string[0]))   // false
);

p.s. Yes, you could use !strlen(trim($string, @$string[0])) to prevent warnings/notices caused by a zero-length string, but I avoid error suppression like the plague because it generally gives code a bad smell.

Upvotes: 1

Barmar
Barmar

Reputation: 780798

You can use a regular expression with a back-reference:

if (preg_match('/^(.)\1*$/', $string)) {
    echo "Same characters";
}

Or a simple loop:

$same = true;
$firstchar = $string[0];
for ($i = 1; $i < strlen($string); $i++) {
    if ($string[$i] != $firstchar) {
        $same = false;
        break;
    }
}

Upvotes: 4

chris85
chris85

Reputation: 23892

You could split on every character then count the array for unique values.

if(count(array_count_values(str_split('abaaaa'))) == 1) {
    echo 'True';
} else {
    echo 'false';
}

Demo: https://eval.in/760293

Upvotes: 9

Symeon Quimby
Symeon Quimby

Reputation: 869

strlen(str_replace($string[0], '', $string)) ? false : true;

Upvotes: 1

rollingBalls
rollingBalls

Reputation: 1878

For the fun of it:

<?php

function str2Dec($string) {
    $hexstr = unpack('H*', $string);
    $hex = array_shift($hexstr);

    return hexdec($hex);
}

function isBoring($string) {
    return str2Dec($string) % str2Dec(substr($string, 0, 1)) === 0;
}

$string1 = 'tttttt';
$string2 = 'ttattt';

var_dump(isBoring($string1)); // => true
var_dump(isBoring($string2)); // => false

Obviously this works only in small strings because once it gets big enough, the INT will overflow and the mod will not produce the correct value. So, don't use this :) - posting it just to show a different idea from the usual ones.

Upvotes: 1

LF-DevJourney
LF-DevJourney

Reputation: 28529

count(array_unique(explode('', string)) == 1) ? true : false;

Upvotes: 5

Related Questions