I have two strings. One of them contains <em> tag, is completely lowercase and doesn't contain delimiters or common words like 'the', 'in', etc. while the other isn't. An example:
$str1 = 'world <em>round</em>';
$str2 = 'World - is Round';
I want to make the $str2 as 'World - is <em>Round</em>', by comparing which lowercase word in the $str1 contains the <em> tag. So far, I've done the following, but is fails if number of words aren't equal in both strings.
public static function applyHighlighingOnDisplayName($str1, $str2) {
$str1_w = explode(' ', $str1);
$str2_w = explode(' ', $str2);
for ($i=0; $i<count($str1_w); $i++) {
if (strpos($str1_w[$i], '<em>') !== false) {
$str2_w[$i] = '<em>' . $str2_w[$i] . '</em>';
}
}
return implode(' ', $str2_w);
}
$str1 = '<em>cup</em> <em>cakes</em>' & $str2 = 'Cup Cakes':
applyHighlighingOnDisplayName($str1, $str2) : '<em>Cup</em> <em>Cakes</em>': Correct
$str1 = 'cup <em>cakes</em>' & $str2 = 'The Cup Cakes':
applyHighlighingOnDisplayName($str1, $str2) : 'The <em>Cup</em> Cakes: Incorrect
How should I change my approach?
preg_replace;<em>word is present?<em></em>.