PHP - 在两种情况下都在数组 return 值的字符串中查找数组的单词
PHP - Find words of array in strings in array return value in both cases
问题
我有两个数组。 1 个数组包含单词,另一个数组包含字符串。我写了一段代码,可以在字符串中找到这个词。
如果在字符串中找到其中一个单词。我return这个词。但是如果在字符串中找到数组中的单词我也想 return 一个值。我已经写了几个代码,但我总是遇到相同的情况。
个案是:
- 没有匹配时我没有得到任何值
- 我从之前的结果中取回了值
- 我得到了每个值匹配但没有匹配回来
问题
我弄错了。如果找到,我如何为每个字符串存储单词,如果找不到,则该值设置为缺失值?
代码
输入
$csv_specie = array("Mouse","Human");
$CDNA = 'Human interleukin 2 (IL2)a;Ampicillin resistance gene (amp)a;Mouse amp gene';
# Split string by ; symbol into an array
$CDNA_new = preg_split("/\b;\b/", $CDNA);
输出(我想以这样的方式结束
foreach ($CDNA_new as $string){
$specie = $result ## Human
echo $specie."-"$sring. "<br \>\n";
}
网络浏览器中的结果:
Human-Human interleukin 2 (IL2)a
NA-Ampicillin resistance gene (amp)a
Mouse-Mouse amp gene
第一次尝试
# Go through the string
foreach($CDNA_new as $t){
# Go through the specie array
foreach ($csv_specie as $c){
# Find specie in string
if (strpos($t, $c) !== FALSE ){
$match = $c;
$specie = $c;
}
}
# If no match found set values to missing values
if (isset($specie) !== TRUE){
$match = "NA";
$specie = "NA";
}
echo "----------------------". "<br \>\n";
echo '+'.$specie. "<br \>\n";
echo '+'.$match. "<br \>\n";
echo '+'.$t. "<br \>\n";
# Work further with the values to retrieve gene ID using eSearch
}
第二次尝试
# use function to find match
function existor_not($str, $character) {
if (strpos($str, $character) !== false) {
return $character;
}
return $character = "0";
}
foreach ( $CDNA_new as $string ){
foreach ( $csv_specie as $keyword ){
$test = existor_not($string,$keyword);
}
echo "-".$test."|" . $string. "<br \>\n";
# Work further with the values to retrieve gene ID using eSearch
}
第三次尝试
foreach ( $CDNA_new as $string ){
foreach ( $csv_specie as $keyword ){
$result = stripos($string, $keyword);
if ($result === false) {
$specie = "NA";
}
else {
$specie = $keyword;
}
}
if ($specie !== "NA"){
echo "match found";
}else{
$match = "NA";
$specie = "NA";
}
echo $specie. "<br \>\n";
# Work further with the values to retrieve gene ID using eSearch
}
仅以您的第一个版本为基础,存在一些问题。您没有重置用于存储匹配项的字段,因此下一次它仍然具有上一个循环的匹配项。
您还使用了 $qspecie
并且设置了 $specie
。
foreach($CDNA_new as $t){
$match = null; // Reset value for match
# Go through the specie array
foreach ($csv_specie as $c){
# Find specie in string
if (strpos($t, $c) !== FALSE ){
$match = $c;
break; // Don't carry on if you found a match
}
}
# If no match found set values to missing values
if ($match == null){
$match = "NA";
}
echo "----------------------". "<br \>\n";
echo '+'.$match. "<br \>\n";
echo '+'.$t. "<br \>\n";
# Work further with the values to retrieve gene ID using eSearch
}
或者您可以依靠设置虚拟值,然后只有在找到真正的匹配项时才会覆盖它...
foreach($CDNA_new as $t){
$match = "NA";
# Go through the specie array
foreach ($csv_specie as $c){
# Find specie in string
if (strpos($t, $c) !== FALSE ){
$match = $c;
break;
}
}
echo "----------------------". "<br \>\n";
echo '+'.$match. "<br \>\n";
echo '+'.$t. "<br \>\n";
# Work further with the values to retrieve gene ID using eSearch
}
您可以使用 preg_grep 在 specie 循环中以不区分大小写的方式进行匹配。
然后我使用 array_diff 从 $cdna 中删除项目以确保我不会再次匹配或浪费时间。
循环后留在$cdna中的是不匹配的项,我将它们添加到"N/A"项中。
$csv_specie = array("Mouse","Human");
$CDNA = 'Human interleukin 2 (IL2)a;Ampicillin resistance gene (amp)a;Mouse amp gene;Some other stuff unknown to man kind';
$csv_specie = array("Mouse","Human");
$CDNA = 'Human interleukin 2 (IL2)a;Ampicillin resistance gene (amp)a;Mouse amp gene;Some other stuff unknown to man kind;some other human stuff';
$cdna = explode(";", $CDNA);
Foreach($csv_specie as $specie){
$matches[$specie] = preg_grep("/\b" . $specie . "\b/i", $cdna);
Echo $specie . " - " . implode("\n" . $specie . " - " , $matches[$specie]) . "\n";
// Remove matched items from $cdna
// This makes $cdna smaller for each
// iteration and make it faster.
$cdna = array_diff($cdna, $matches[$specie]);
}
// What is left in $cdna is not matched
$matches["N/A"] = $cdna;
Echo "\nN/A - " . implode("\nN/A - ", $matches["N/A"]);
输出:
Mouse - Mouse amp gene
Human - Human interleukin 2 (IL2)a
Human - some other human stuff
N/A - Ampicillin resistance gene (amp)a
N/A - Some other stuff unknown to man kind
问题
我有两个数组。 1 个数组包含单词,另一个数组包含字符串。我写了一段代码,可以在字符串中找到这个词。 如果在字符串中找到其中一个单词。我return这个词。但是如果在字符串中找到数组中的单词我也想 return 一个值。我已经写了几个代码,但我总是遇到相同的情况。
个案是:
- 没有匹配时我没有得到任何值
- 我从之前的结果中取回了值
- 我得到了每个值匹配但没有匹配回来
问题
我弄错了。如果找到,我如何为每个字符串存储单词,如果找不到,则该值设置为缺失值?
代码
输入$csv_specie = array("Mouse","Human");
$CDNA = 'Human interleukin 2 (IL2)a;Ampicillin resistance gene (amp)a;Mouse amp gene';
# Split string by ; symbol into an array
$CDNA_new = preg_split("/\b;\b/", $CDNA);
输出(我想以这样的方式结束
foreach ($CDNA_new as $string){
$specie = $result ## Human
echo $specie."-"$sring. "<br \>\n";
}
网络浏览器中的结果:
Human-Human interleukin 2 (IL2)a
NA-Ampicillin resistance gene (amp)a
Mouse-Mouse amp gene
第一次尝试
# Go through the string
foreach($CDNA_new as $t){
# Go through the specie array
foreach ($csv_specie as $c){
# Find specie in string
if (strpos($t, $c) !== FALSE ){
$match = $c;
$specie = $c;
}
}
# If no match found set values to missing values
if (isset($specie) !== TRUE){
$match = "NA";
$specie = "NA";
}
echo "----------------------". "<br \>\n";
echo '+'.$specie. "<br \>\n";
echo '+'.$match. "<br \>\n";
echo '+'.$t. "<br \>\n";
# Work further with the values to retrieve gene ID using eSearch
}
第二次尝试
# use function to find match
function existor_not($str, $character) {
if (strpos($str, $character) !== false) {
return $character;
}
return $character = "0";
}
foreach ( $CDNA_new as $string ){
foreach ( $csv_specie as $keyword ){
$test = existor_not($string,$keyword);
}
echo "-".$test."|" . $string. "<br \>\n";
# Work further with the values to retrieve gene ID using eSearch
}
第三次尝试
foreach ( $CDNA_new as $string ){
foreach ( $csv_specie as $keyword ){
$result = stripos($string, $keyword);
if ($result === false) {
$specie = "NA";
}
else {
$specie = $keyword;
}
}
if ($specie !== "NA"){
echo "match found";
}else{
$match = "NA";
$specie = "NA";
}
echo $specie. "<br \>\n";
# Work further with the values to retrieve gene ID using eSearch
}
仅以您的第一个版本为基础,存在一些问题。您没有重置用于存储匹配项的字段,因此下一次它仍然具有上一个循环的匹配项。
您还使用了 $qspecie
并且设置了 $specie
。
foreach($CDNA_new as $t){
$match = null; // Reset value for match
# Go through the specie array
foreach ($csv_specie as $c){
# Find specie in string
if (strpos($t, $c) !== FALSE ){
$match = $c;
break; // Don't carry on if you found a match
}
}
# If no match found set values to missing values
if ($match == null){
$match = "NA";
}
echo "----------------------". "<br \>\n";
echo '+'.$match. "<br \>\n";
echo '+'.$t. "<br \>\n";
# Work further with the values to retrieve gene ID using eSearch
}
或者您可以依靠设置虚拟值,然后只有在找到真正的匹配项时才会覆盖它...
foreach($CDNA_new as $t){
$match = "NA";
# Go through the specie array
foreach ($csv_specie as $c){
# Find specie in string
if (strpos($t, $c) !== FALSE ){
$match = $c;
break;
}
}
echo "----------------------". "<br \>\n";
echo '+'.$match. "<br \>\n";
echo '+'.$t. "<br \>\n";
# Work further with the values to retrieve gene ID using eSearch
}
您可以使用 preg_grep 在 specie 循环中以不区分大小写的方式进行匹配。
然后我使用 array_diff 从 $cdna 中删除项目以确保我不会再次匹配或浪费时间。
循环后留在$cdna中的是不匹配的项,我将它们添加到"N/A"项中。
$csv_specie = array("Mouse","Human");
$CDNA = 'Human interleukin 2 (IL2)a;Ampicillin resistance gene (amp)a;Mouse amp gene;Some other stuff unknown to man kind';
$csv_specie = array("Mouse","Human");
$CDNA = 'Human interleukin 2 (IL2)a;Ampicillin resistance gene (amp)a;Mouse amp gene;Some other stuff unknown to man kind;some other human stuff';
$cdna = explode(";", $CDNA);
Foreach($csv_specie as $specie){
$matches[$specie] = preg_grep("/\b" . $specie . "\b/i", $cdna);
Echo $specie . " - " . implode("\n" . $specie . " - " , $matches[$specie]) . "\n";
// Remove matched items from $cdna
// This makes $cdna smaller for each
// iteration and make it faster.
$cdna = array_diff($cdna, $matches[$specie]);
}
// What is left in $cdna is not matched
$matches["N/A"] = $cdna;
Echo "\nN/A - " . implode("\nN/A - ", $matches["N/A"]);
输出:
Mouse - Mouse amp gene
Human - Human interleukin 2 (IL2)a
Human - some other human stuff
N/A - Ampicillin resistance gene (amp)a
N/A - Some other stuff unknown to man kind