Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

294
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!