Use the first string (whitelist string) as the mask fortrim()
. This will remove all matched characters from the second string. If there are any characters remaining after the trim, thenfalse
.
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 thatstr_replace()
is famous for, but without needing to split the whitelist string into an array first. I don't know ifltrim()
orrtrim()
would be any faster (on a microscopic level) thantrim()
, 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 ofstrlen()
.
!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.
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);
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));
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.