How to separate (with white space) strings when it's glued, using keys in arrays to check if it's glued?
glued: sisteralannis
, goodplace
(replace for: sister alannis
, good place
)
Note: both have the starting part of existing keys in the array: sister, good, but they are not exactly keys, so replacing can not occur, so I need to separate them away, so that replacement is possible in the next step of the script. another solution would be to remove everything that is not exactly the same as the keys in $myWords
This code is to replace strings, I want an improvement, a code that verifies if the strings are glued, and add a space between them, separating them:
$myVar = "my sisteralannis is not that blonde, here is a goodplace";
$myWords=array(
array("is","é"),
array("on","no"),
array("that","aquela"),
array("sister","irmã"),
array("my","minha"),
array("myth","mito"),
array("he","ele"),
array("good","bom"),
array("ace","perito")
);
usort($myWords,function($a,$b){return mb_strlen($b[0])<=>mb_strlen($a[0]);}); // sort subarrays by first column multibyte length
// remove mb_ if first column holds no multi-byte characters. strlen() is much faster.
foreach($myWords as &$words){
$words[0]='/\b'.$words[0].'\b/ui'; // generate patterns using search word, word boundaries, and case-insensitivity
}
$myVar=preg_replace(array_column($myWords,0),array_column($myWords,1),$myVar);
//APPLY SECOND SOLUTION HERE
echo $myVar;
Expected Output: minha irmã alannis é not aquela blonde, here é a bom place
.
=================
2ª solution More Simple: make match between $myVar and $myWords and delete anything that does not exist in $myWords.
would be to delete all strings of the variable that are not found in the array!
output: minha é aquela, é