Evaluate an ordered array for judging the appearance of array values in a string
$ Arr = array ('A', 'M', 'n ');
The string is not fixed, but M and N can only appear once in the string. if they appear multiple times, false is returned.
Returned array $ data ['M'], $ data ['n']
For example:
A man returns 2, 4 // M for the first appearance in the array, and N for the second appearance in the array (A appears in 1 and 3)
A boy returns false // M or N is not included
MAN returns 1, 3 // M as the first occurrence of the array, and N as the first occurrence of the array (A appears in 2)
A atm in returns 3, 5 // M, which is 3rd IN the array, and N is 4th IN the array (A appears IN 1 and 2)
Reply to discussion (solution)
A simple question has become complicated as you have explained it.
Read the results of this code and you will know what to do.
$arr = array('A','M','N');$data = array( 'A MAN', 'A BOY', 'MAN', 'A ATM IN');foreach($data as $text) { $t = str_split($text); $r[] = array_intersect($t, $arr);}print_r($r);
Array( [0] => Array ( [0] => A [2] => M [3] => A [4] => N ) [1] => Array ( [0] => A ) [2] => Array ( [0] => M [1] => A [2] => N ) [3] => Array ( [0] => A [2] => A [4] => M [7] => N ))
A simple question has become complicated as you have explained it.
Read the results of this code and you will know what to do.
$arr = array('A','M','N');$data = array( 'A MAN', 'A BOY', 'MAN', 'A ATM IN');foreach($data as $text) { $t = str_split($text); $r[] = array_intersect($t, $arr);}print_r($r);
Array( [0] => Array ( [0] => A [2] => M [3] => A [4] => N ) [1] => Array ( [0] => A ) [2] => Array ( [0] => M [1] => A [2] => N ) [3] => Array ( [0] => A [2] => A [4] => M [7] => N ))
Thank you for your enthusiastic answers.
But it doesn't mean that the result displayed by your code is not what I want,
The first three results of the array are correct because the result is a coincidence. The fourth result is an error.
It may be that my expression is confusing.
$arr = array('(title)','(url)','(img)');$str = '';
What I want to get now is (the title is 2nd), the url is 1st, and the img is 3rd.
The following string
$str = '
(title)';
What I want to get now is (title is the second or second appearance), url is 1st, and img is 4th.
$ Arr = array ('A', 'M', 'n ');
$ Str = 'amn ';
$ Data = array ();
Foreach ($ arr as $ v ){
If (substr_count ($ str, $ v) = 1 ){
$ Data [$ v] = strpos ($ str, $ v );
}
}
Var_dump ($ data );
$ Arr = array ('A', 'M', 'n ');
$ Str = 'amn ';
$ Data = array ();
Foreach ($ arr as $ v ){
If (substr_count ($ str, $ v) = 1 ){
$ Data [$ v] = strpos ($ str, $ v );
}
}
Var_dump ($ data );
If $ str = 'amtn ', an error will occur.
$ Arr = array ('A', 'M', 'n ');
$ Str = 'amn ';
$ Data = array ();
Foreach ($ arr as $ v ){
If (substr_count ($ str, $ v) = 1 ){
$ Data [$ v] = strpos ($ str, $ v );
}
}
Var_dump ($ data );
If $ str = 'amtn ', an error will occur.
No error
Array (3) {["A"] => int (0) ["M"] => int (1) ["N"] => int (3 )}
$ Arr = array ('A', 'M', 'n ');
$ Str = 'amn ';
$ Data = array ();
Foreach ($ arr as $ v ){
If (substr_count ($ str, $ v) = 1 ){
$ Data [$ v] = strpos ($ str, $ v );
}
}
Var_dump ($ data );
If $ str = 'amtn ', an error will occur.
No error
Array (3) {["A"] => int (0) ["M"] => int (1) ["N"] => int (3 )}
M is the second player. Result 1 and N is the first player. it should be 2.
There were three major singers and a bunch of little stars at the concert.
Major singers are: Liu Dehua, Zhang Xueyou, and Lin Junjie
The organizer arranged the order of appearance. Lin Junjie sang a few more songs while the other two sang only one song.
Zhang Xueyou, XXX, XXX, Lin Junjie, XXX, Lin Junjie, XXX, XXX, Liu Dehua, XXX, Lin Junjie XXX
What I need now is the order of three of them
The result should be Lin Junjie 2, 3, 5
Zhang Xueyou 1
Andy Lau 4
I don't know.
$arr = array('A','M','N');$str = 'AMTN';$data = array();foreach($arr as $v){if(substr_count($str,$v) == 1){$data[$v] = strpos($str,$v)+1;}}var_dump($data);
Result
Array (3) {["A"] => int (1) ["M"] => int (2) ["N"] => int (4 )}
$arr = array('A','M','N');$str = 'AMTN';$data = array();foreach($arr as $v){if(substr_count($str,$v) == 1){$data[$v] = strpos($str,$v)+1;}}var_dump($data);
Result
Array (3) {["A"] => int (1) ["M"] => int (2) ["N"] => int (4 )}
The result n I want is 3.
I thought you would just count the order of AMN. I reversed the array key and value to see if I could get the result.
Done. it is matched with the regular expression. Alas !~
$arr = array('(title)','(url)','(img)');$str = '(title)';preg_match_all('/(\(title\)|\(url\)|\(img\))/i',$str,$data);print_r($data);