如何判断两组字符串是否有相同的字符?
$a=2344;//主字符串,固定值。
//下面为可变化的变量
如果$b=12;//判断$a字符串中是否包含1和2,判断后结果为假
如果$b=22;//判断$a字符串中是否包含两个2,判断后结果只有一个2,假
如果$b=234;//判断$a字符串中是否包含2,3,4.判断后结果$a中全部包含,真
如果$b=324;//判断$a字符串中是否包含3,2,4.判断后结果$a中全部包含,真
如果$b=4432;//判断$a字符串中是否包含4,4,3,2.判断后结果$a中全部包含,真
如果$b=4444;//判断$a字符串中是否包含4个4,.判断后结果$a只有二个4,假
如果$b
如果$b
如果$b
......后面还会有许多
我需要的判断结果为如上。
判断语句怎么写?
回复讨论(解决方案)
$a = '2344';$d = array('12', '22', '234', '324', '4432', '4444');$a = count_chars($a, 1);foreach($d as $b) { $t = count_chars($b, 1); $f = 0; if(array_keys(array_intersect_key($t, $a)) == array_keys($a)) $f = 1; printf("b = %s %s\n", $b, $f ? '真' : '假');}
b = 12 假b = 22 假b = 234 真b = 324 真b = 4432 真b = 4444 假
如果$b=4444;//判断$a字符串中是否包含4个4,.判断后结果$a只有二个4,假
这条规则似乎不妥
把$a和$b按字符分别拆成数组,然后判断是否子集?
不知道你这样做的目的是什么?
要从业务逻辑入手,可能有更好的解决方案。
$a = '2344';$d = array('12', '22', '234', '324', '4432', '4444');$a = count_chars($a, 1);foreach($d as $b) { $t = count_chars($b, 1); $f = 0; if(array_keys(array_intersect_key($t, $a)) == array_keys($a)) $f = 1; printf("b = %s %s\n", $b, $f ? '真' : '假');}
b = 12 假b = 22 假b = 234 真b = 324 真b = 4432 真b = 4444 假
如果$b=4444;//判断$a字符串中是否包含4个4,.判断后结果$a只有二个4,假
这条规则似乎不妥
这样的判断还是有缺陷啊,
如果$a为4444呢。
我想要的结果是,只有$b=4444;或者$b=44,或者$b=444或者$b=4的时候,才能匹配成功,才能为真,其他都不符合,
我已经说了,你的规则有问题(或者说不完备)
我给的代码只能处理一种情况:$a 中的所有字符(去除重复的)都必须出现在 $b 中
等你制定好完备的规则后,再去扩展功能
先把字符串转换成数组,然后用array_count_values计算各字符出现的字数,如果$b中出现的次数小于等于$a中出现的字数就是真,否则就是假。
我已经说了,你的规则有问题(或者说不完备)
我给的代码只能处理一种情况:$a 中的所有字符(去除重复的)都必须出现在 $b 中
等你制定好完备的规则后,再去扩展功能
直接说我的业务需求吧,或许能明白一点,
这样子的。
七星彩你应该知道吧,
我们公司需要做一个这样的一个游戏活动。
如果这期开奖号码是5667 321
这里我们只截取前面四位数,也就是5667来作为是否中奖,后面三位省略掉。
而中奖规则为四等奖项。
一等奖为四个数同现,意思是,四个数字同时出现
二等奖为三个数同现,
三等奖为二个数同现,
四等奖为一个数同现。
那么, 开奖之前,会有许多人来投注。
如果a单投了一个5,则开奖号码中有一个5,则中奖了。但他中的是四等奖。
如果B投了567,则开奖号码中同时出现567,中了个2等奖。
如果C投了66,则开奖号码中,有两个6同时出现,也中了。(可能会问,为什么这样投呢,因为如果投重复码并中奖的话,奖励会翻倍。)
如果D投了5676,则开奖号码中全部匹配,中了一等奖。
大概是这样的一个规则。意思是,不过你投了1个数字或者4个数字,开奖号码中,必须全部匹配才能中奖。包括重复数
$a='5415';$a=str_split($a);$a=array_count_values($a);$b='514';$b=str_split($b);$b=array_count_values($b);$result=true;foreach($b as $k=>$v){ if(!isset($a[$k]) OR $a[$k]<$v){ $result=false; break; }}var_dump($result);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

Go language is a powerful and flexible programming language that provides rich string processing functions, including string interception. In the Go language, we can use slices to intercept strings. Next, we will introduce in detail how to intercept strings in Go language, with specific code examples. 1. Use slicing to intercept a string. In the Go language, you can use slicing expressions to intercept a part of a string. The syntax of slice expression is as follows: slice:=str[start:end]where, s

Title: How to determine whether a string ends with a specific character in Golang. In the Go language, sometimes we need to determine whether a string ends with a specific character. This is very common when processing strings. This article will introduce how to use the Go language to implement this function, and provide code examples for your reference. First, let's take a look at how to determine whether a string ends with a specified character in Golang. The characters in a string in Golang can be obtained through indexing, and the length of the string can be

How to check if a string starts with a specific character in Golang? When programming in Golang, you often encounter situations where you need to check whether a string begins with a specific character. To meet this requirement, we can use the functions provided by the strings package in Golang to achieve this. Next, we will introduce in detail how to use Golang to check whether a string starts with a specific character, with specific code examples. In Golang, we can use HasPrefix from the strings package

Methods to solve Chinese garbled characters when converting hexadecimal strings in PHP. In PHP programming, sometimes we encounter situations where we need to convert strings represented by hexadecimal into normal Chinese characters. However, in the process of this conversion, sometimes you will encounter the problem of Chinese garbled characters. This article will provide you with a method to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP, and give specific code examples. Use the hex2bin() function for hexadecimal conversion. PHP’s built-in hex2bin() function can convert 1

Question: How to determine whether the date is the previous day in Go language? In daily development, we often encounter situations where we need to determine whether the date is the previous day. In the Go language, we can implement this function through time calculation. The following will be combined with specific code examples to demonstrate how to determine whether the date is the previous day in Go language. First, we need to import the time package in the Go language. The code is as follows: import("time") Then, we define a function IsYest

Strings in GoLang, although immutable, can be dynamically modified using the following technique: concatenating strings using string concatenation. Create a new string using string formatting. Modify the underlying byte slice of the string. Use mutable string types provided by third-party libraries.
