Php 在';使用数组中的键检查它是否';它粘在一起了吗?
如何在粘贴字符串时(使用空格)分隔字符串,使用数组中的键检查是否已粘贴 粘合:Php 在';使用数组中的键检查它是否';它粘在一起了吗?,php,arrays,replace,whitespace,Php,Arrays,Replace,Whitespace,如何在粘贴字符串时(使用空格)分隔字符串,使用数组中的键检查是否已粘贴 粘合:sisteralannis,goodplace(替换为:sister alannis,good place) 注意:两者都有数组中现有键的起始部分:sister,很好,但它们并不完全是键,因此无法进行替换,因此我需要将它们分开,以便在脚本的下一步中可以进行替换。另一个解决方案是删除与$myWords中的键不完全相同的所有内容 这段代码是用来替换字符串的,我需要一个改进,一个验证字符串是否粘合的代码,并在它们之间添加一个
sisteralannis
,goodplace
(替换为:sister alannis
,good place
)
注意:两者都有数组中现有键的起始部分:sister,很好,但它们并不完全是键,因此无法进行替换,因此我需要将它们分开,以便在脚本的下一步中可以进行替换。另一个解决方案是删除与$myWords中的键不完全相同的所有内容
这段代码是用来替换字符串的,我需要一个改进,一个验证字符串是否粘合的代码,并在它们之间添加一个空格,分隔它们:
$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;
$myVar=“我的姐姐不是那么金发,这里是个好地方”;
$myWords=array(
数组(“is”,“é”),
数组(“on”、“no”),
数组(“该”、“aquela”),
数组(“姐妹”、“irmã”),
数组(“my”、“minha”),
阵列(“神话”,“水户”),
数组(“he”、“ele”),
数组(“良好”、“bom”),
数组(“ace”、“perito”)
);
usort($myWords,function($a,$b){return mb_strlen($b[0])mb_strlen($a[0]);};//按第一列多字节长度对子数组排序
//如果第一列不包含多字节字符,请删除mb。strlen()要快得多。
foreach($myWords as&$words){
$words[0]='/\b'.$words[0].\b/ui';//使用搜索词、词边界和大小写不敏感度生成模式
}
$myVar=preg_replace(数组_列($myWords,0),数组_列($myWords,1),$myVar);
//在这里应用第二种解决方案
echo$myVar;
预期输出:minha irmãalannisénot aquela blonde,这里是一个bom位置
=================
2ª解决方案更简单:
在$myVar和$myWords之间进行匹配,并删除$myWords中不存在的任何内容
将删除数组中未找到的所有变量字符串
输出:
minhaéaquela,é
我不能说我100%相信这将处理所有可能的情况,但它确实适用于您的输入字符串,我构建它是为了适应首字母大写的单词。除此之外,可能还有一些边缘案例需要调整
有一些内联解释可以帮助理解代码
代码:()
您如何知道如何将
goodplace
转换为bom place
,而不是在此处将转换为ele-re
?换句话说,是什么使goodplace
成为一个“胶粘”单词,而在这里却不是,当两者都在$myWords
列表中有它们的起始部分时?strtrtr()
会弄坏字符串@salathe是正确的,这在逻辑上是无法做到的。Ariane,请让这个任务可以解决。我想我不太明白如果不可能做到这一点,另一件事将解决,查询数组,并从变量中删除数组中不存在的所有内容,输出:minhaéaquela,é
搜索$myVar和$myWords之间的匹配如果$myWords中不存在,则结束删除此代码工作得非常完美(感谢mickmackusa),但我没有指望使用该系统的数千名用户的拼写错误会产生如此多的粘合词,因此在进行替换之前,我需要分离这些词,或者删除与$myWords不匹配的所有内容
$myVar = "My sisteralannis is not that blonde, here is a goodplace";
$myWords=[["is","é"],["on","no"],["that","aquela"],["sister","irmã"],["my","minha"],
["myth","mito"],["he","ele"],["good","bom"],["ace","perito"]];
usort($myWords,function($a,$b){return strlen($b[0])<=>strlen($a[0]);}); // longer English words before shorter
$search=array_column($myWords,0); // cache for multiple future uses
//input: "My sisteralannis is not that blonde, here is a goodplace";
//filter: ++ ------------- ++ --- ++++ ------ ---- ++ - ---------
//output: Minha é aquela , é
$disqualifying_pattern='/ ?\b(?>'.implode('|',$search).')\b(*SKIP)(*FAIL)| ?[a-z]+/i'; // this handles the spaces for the sample input, might not work for all cases
//echo $disqualifying_pattern,"\n";
$filtered=preg_replace($disqualifying_pattern,'',$myVar);
//echo $filtered,"\n";
$patterns=array_map(function($v){return '/\b'.$v.'\b/i';},$search);
$replace=array_column($myWords,1);
echo preg_replace_callback(
$patterns,
function($m)use($patterns,$replace){
$new=preg_replace($patterns,$replace,$m[0],1); // tell it to stop after replacing once
if(ctype_upper($m[0][0])){ // if first letter of English word is uppercase
$mb_ucfirst=mb_strtoupper(mb_substr($new,0,1)); // target and make upper, first letter of Portugese word
return $mb_ucfirst.mb_substr($new, 1); // apply new uppercase letter to the rest of the Portugese word
}
return $new;
},
$filtered
);
Minha é aquela, é