I created the following if loop with php (I'm a php beginner), with which I can search for a specific search term in the text in an ACF textfield and output a text depending on the result:
This is my code for finding one keyword only and it works fine:
$text = get_field('my_acf_field');
$keyword = "text";
$result = strpos($text, $keyword);
if($result === false) {
echo "Text not found";
} else {
echo "Text found";
}
This works perfectly. Now I would like to extend the code to have several search words output differently. So something along the lines of:
$text = get_field('my_acf_field');
$keyword1 = "text";
$keyword2 = "text 2";
$keyword3 = "text 3";
$result = strpos($text, $keyword1, $keyword2, $keyword3);
if($result === $keyword1) {
echo "Lorem Ipsum 1";
} elseif($result === $keyword2) {
echo "Lorem Ipsum 2";
} elseif($result === $keyword3) {
echo "Lorem Ipsum 3";
} else {
echo "Text not found";
}
However, that doesn't work and I can't find my mistake.
Thank you for your help
Mags
$foundKeywords = [];
foreach ([$keyword1, $keyword2, $keyword3] as $keyword) {
if (strpos($text, $keyword) !== false) {
echo 'Found keyword: ' . $keyword;
$foundKeywords[] = $keyword;
}
}
if (empty($foundKeywords)) {
echo 'No keywords found';
}