我有兩個字符串如何比較字符串與PHP中的特殊字符?
$string1 = 'Amateur developer | Photoshop lover| Alcohol scholar | Internet practitioner';
和
$string2 = 'Amateur developer | Photoshop lover| Alcohol scholar';
如何PHP
與之間的特殊字符(空格和連字符)這兩個字符串比較?
我有兩個字符串如何比較字符串與PHP中的特殊字符?
$string1 = 'Amateur developer | Photoshop lover| Alcohol scholar | Internet practitioner';
和
$string2 = 'Amateur developer | Photoshop lover| Alcohol scholar';
如何PHP
與之間的特殊字符(空格和連字符)這兩個字符串比較?
試試這個與案例相比較;
$result = strcmp($string1, $string2);
試試這個比較沒有案例consier;
$result = strcasecmp($string1, $string2);
如果$ result是0(零),那麼字符串相等,否則在兩種情況下都不相等。
我不知道爲什麼有人一票倒下這個答案??? 這個答案有什麼問題?請簡單解釋一下。 –
你的努力是好的,但你的答案是不恰當的,有人投票,因爲你的答案只能比較沒有特殊字符的字符串。用戶想要比較其中包含特殊字符的字符串。 –
試試這個
$result = strcmp($string1, $string2);
我會建議使用傑卡德指數,看到這一點:https://gist.github.com/henriquea/540303
<?php
function getSimilarityCoefficient($item1, $item2, $separator = ",") {
$item1 = explode($separator, $item1);
$item2 = explode($separator, $item2);
$arr_intersection = array_intersect($item2, $item2);
$arr_union = array_merge($item1, $item2);
$coefficient = count($arr_intersection)/count($arr_union);
return $coefficient;
}
$string2 = 'Amateur developer | Photoshop lover | Alcohol scholar | Internet practitioner';
$string2 = 'Amateur developer | Photoshop lover | Alcohol scholar';
echo getSimilarityCoefficient($string1,$string2,' | ');
?>
如果他們一直通過管道(|
)分開,你只想要一個向下和髒檢查:
// original strings
$str1 = 'Amateur developer | Photoshop lover| Alcohol scholar | Internet practitioner';
$str2 = 'Amateur developer | Photoshop lover| Alcohol scholar';
// split them by the pipe
$exp1 = explode('|', $str1);
$exp2 = explode('|', $str2);
// trim() them to remove excess whitespace
$trim1 = array_map('trim', $exp1);
$trim2 = array_map('trim', $exp2);
// you could also array_map them to strtolower
// to take CaSE out of the equation
Then:
// MATCHING ENTRIES
$same = array_intersect($trim1, $trim2);
var_dump($same);
// DIFFERENT ENTRIES
$diff = array_diff($trim1, $trim2);
var_dump($diff);
使用此similar_text() - 計算兩個字符串
之間的相似性尋找這種比較?
<?php
$string1 = 'Amateur developer | Photoshop lover| Alcohol scholar | Internet practitioner';
$string2 = 'Amateur developer | Photoshop lover| Alcohol scholar';
if ($string1 == $string2) {
echo "Strings are same";
} else {
$stringArray1 = explode(' | ', $string1);
$stringArray2 = explode(' | ', $string2);
$diffAre = array_diff($stringArray1, $stringArray2);
echo "Difference in strings are " . implode($diffAre, ',');
}
?>
輸出
Difference in strings are Internet practitioner
請更確切地說明你到底想要什麼。您當然可以使用通常的操作符比較兩個字符串與這些字符。 – Joey
因爲你的問題是...... if($ string1 === $ string2){} –
@JamieTaylor ===' –