Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

295
Visualizações
Check if a string is solely composed of characters found in another string

Considering sample data such as:

$str1 = "apple";
$str2 = "pal";

I want to return a true response only if all the characters [alphabetical characters] of the $str2 are also present in $str1, otherwise false.

Since all letters p, a, and l all exist in $str1, then result should be true.

If $str2 was pole, then the p, l, and e would be found, but the o would not. This scenario should return false.

My pseudo code is:

if (str2 is present in str1){
    // true;
    // since all characters of str2 are also in str1, so it should return true.
} else{
    // false;
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Use the first string (whitelist string) as the mask for trim(). This will remove all matched characters from the second string. If there are any characters remaining after the trim, then false.

since all characters of str2 are also in str1, so it should return true

Code: (Demo)

$str1 = "apple";
$str2 = "pal";
$str3 = "pole";
var_export(!strlen(trim($str2, $str1)));
echo "\n---\n";
var_export(!strlen(trim($str3, $str1)));

Output:

true
---
false

This answer is clean and direct because it does not need to generate temporary arrays for subsequent comparison. trim()'s character mask affords the same action that str_replace() is famous for, but without needing to split the whitelist string into an array first. I don't know if ltrim() or rtrim() would be any faster (on a microscopic level) than trim(), but any of these functions will deliver the same output.


p.s. If you are guaranteed to only be working with letters, then you can use a falsey check instead of strlen().

!trim($str2, $str1)

I say this because if you allowed numbers, then a trimmed string containing a zero would be considered falsey and return an incorrect result.

over 4 years ago · Santiago Trujillo Relatório

0

You can use the array_intersect function after splitting each characters from a word using the str_split function as:

<?php

$a = "Apple";
$b = "pal";

$matched = !empty(array_intersect(str_split($b), str_split($a))); 

array_intersect will return [ p, l ] ignoring 'A' due to case sensitive

echo $matched; // true

?>

In case if you want to match ignoring the case of the characters then you can first lowercase the word using the strtolower function: and then split as:

$a = "Apple";

$a = strtolower($a);
over 4 years ago · Santiago Trujillo Relatório

0

You can use count_chars() to get array of present chars in each string and then compare those two arrays:

$s1 = 'apple';
$s2 = 'pal';

$c1 = count_chars($s1, 1);
$c2 = count_chars($s2, 1);

$ok = empty(array_diff_key($c2, $c1));
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda