Problem
I have two arrays. 1 array contains words, the other array contains strings. I have written a code that will find the word in the string. If one of the words is found in the string. I return the word. But if the no of the words in the array are found in the string I also would like to return one value. I have written several codes and I end up always with same cases.
The cases are:
- I got no value when there was no match
- I got the value back from the result before
- I got every value match and no match back
Question
I got it somewhere wrong. How could I store for each string the word if found and if not found the value is set to missing value?
Code
Input$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);
Output (I would like to end with something like this
foreach ($CDNA_new as $string){
$specie = $result ## Human
echo $specie."-"$sring. "<br \>\n";
}
Result in web browser:
Human-Human interleukin 2 (IL2)a
NA-Ampicillin resistance gene (amp)a
Mouse-Mouse amp gene
First try
# 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
}
Second try
# 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
}
Third try
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
}