Hướng dẫn php compare two strings and return the difference - php so sánh hai chuỗi và trả về sự khác biệt

Tôi không biết bất cứ điều gì như vậy, vì vậy đây là những gì tôi sẽ làm.

$a = "php freaks";
$b = "phe";
$b = explode('',$b);
foreach ($b as $c) {
     $a = str_replace($c, '', $a);
}
echo $a;

Tôi nghĩ rằng điều đó nên làm việc.

Chỉnh sửa - hoặc cái này:

$a = "php freaks";
$b = "phe";
$c = strlen($b);
for ($e = 0; $e < $c; $e++) {
     $a = str_replace($b[$e], '', $a);
}
echo $a;

Tôi tin rằng nó cũng hoạt động.

$a = "what go around, come around";
$b = "what goes around, comes around";

Khi so sánh $a$b, nó sẽ trả lại một mảng $arr = {"goes","comes"} từ $b khác với $a. Vui lòng đề xuất một giải pháp

Hướng dẫn php compare two strings and return the difference - php so sánh hai chuỗi và trả về sự khác biệt

Prafulla

6007 Huy hiệu bạc18 Huy hiệu Đồng7 silver badges18 bronze badges

Đã hỏi ngày 7 tháng 10 năm 2015 lúc 10:29Oct 7, 2015 at 10:29

Hướng dẫn php compare two strings and return the difference - php so sánh hai chuỗi và trả về sự khác biệt

5

Chỉ cần sử dụng

$a = "php freaks";
$b = "phe";
$c = strlen($b);
for ($e = 0; $e < $c; $e++) {
     $a = str_replace($b[$e], '', $a);
}
echo $a;
1 cùng với
$a = "php freaks";
$b = "phe";
$c = strlen($b);
for ($e = 0; $e < $c; $e++) {
     $a = str_replace($b[$e], '', $a);
}
echo $a;
2 như

$a = "what go around, come around";
$b = "what goes around, comes around";
echo implode(',',array_diff(preg_split('/\h/',$b),preg_split('/\h/',$a)));

Output:

goes,comes

Thử nghiệm

Đã trả lời ngày 7 tháng 10 năm 2015 lúc 10:52Oct 7, 2015 at 10:52

Hướng dẫn php compare two strings and return the difference - php so sánh hai chuỗi và trả về sự khác biệt

Hãy thử chơi xung quanh với hàm php Array_diff ().

array array_diff ( array $array1 , array $array2 [, array $... ] )

Nó so sánh

$a = "php freaks";
$b = "phe";
$c = strlen($b);
for ($e = 0; $e < $c; $e++) {
     $a = str_replace($b[$e], '', $a);
}
echo $a;
3 với một hoặc nhiều mảng khác và trả về các giá trị trong
$a = "php freaks";
$b = "phe";
$c = strlen($b);
for ($e = 0; $e < $c; $e++) {
     $a = str_replace($b[$e], '', $a);
}
echo $a;
3 không có trong bất kỳ mảng nào khác.

Đã trả lời ngày 7 tháng 10 năm 2015 lúc 10:33Oct 7, 2015 at 10:33

Hướng dẫn php compare two strings and return the difference - php so sánh hai chuỗi và trả về sự khác biệt

(Php 4, Php 5, Php 7, Php 8)

STRCMP - So sánh chuỗi an toàn nhị phânBinary safe string comparison

Sự mô tả

strcmp (chuỗi

$a = "php freaks";
$b = "phe";
$c = strlen($b);
for ($e = 0; $e < $c; $e++) {
     $a = str_replace($b[$e], '', $a);
}
echo $a;
5, chuỗi
$a = "php freaks";
$b = "phe";
$c = strlen($b);
for ($e = 0; $e < $c; $e++) {
     $a = str_replace($b[$e], '', $a);
}
echo $a;
6): int
(string
$a = "php freaks";
$b = "phe";
$c = strlen($b);
for ($e = 0; $e < $c; $e++) {
     $a = str_replace($b[$e], '', $a);
}
echo $a;
5
, string
$a = "php freaks";
$b = "phe";
$c = strlen($b);
for ($e = 0; $e < $c; $e++) {
     $a = str_replace($b[$e], '', $a);
}
echo $a;
6
): int

Lưu ý rằng so sánh này là trường hợp nhạy cảm.

Thông số

$a = "php freaks";
$b = "phe";
$c = strlen($b);
for ($e = 0; $e < $c; $e++) {
     $a = str_replace($b[$e], '', $a);
}
echo $a;
7

Chuỗi đầu tiên.

$a = "php freaks";
$b = "phe";
$c = strlen($b);
for ($e = 0; $e < $c; $e++) {
     $a = str_replace($b[$e], '', $a);
}
echo $a;
8

Chuỗi thứ hai.

Trả về giá trị

Trả về

$a = "php freaks";
$b = "phe";
$c = strlen($b);
for ($e = 0; $e < $c; $e++) {
     $a = str_replace($b[$e], '', $a);
}
echo $a;
9 nếu
$a = "php freaks";
$b = "phe";
$c = strlen($b);
for ($e = 0; $e < $c; $e++) {
     $a = str_replace($b[$e], '', $a);
}
echo $a;
7 nhỏ hơn
$a = "php freaks";
$b = "phe";
$c = strlen($b);
for ($e = 0; $e < $c; $e++) {
     $a = str_replace($b[$e], '', $a);
}
echo $a;
8;
$a = "what go around, come around";
$b = "what goes around, comes around";
2 Nếu
$a = "php freaks";
$b = "phe";
$c = strlen($b);
for ($e = 0; $e < $c; $e++) {
     $a = str_replace($b[$e], '', $a);
}
echo $a;
7 lớn hơn
$a = "php freaks";
$b = "phe";
$c = strlen($b);
for ($e = 0; $e < $c; $e++) {
     $a = str_replace($b[$e], '', $a);
}
echo $a;
8 và
$a = "what go around, come around";
$b = "what goes around, comes around";
5 nếu chúng bằng nhau.

Thay đổi

Phiên bảnSự mô tả
8.2.0 Hàm này hiện trả về
$a = "php freaks";
$b = "phe";
$c = strlen($b);
for ($e = 0; $e < $c; $e++) {
     $a = str_replace($b[$e], '', $a);
}
echo $a;
9 hoặc
$a = "what go around, come around";
$b = "what goes around, comes around";
2, trong đó trước đó nó đã trả về một số âm hoặc dương.

Ví dụ

Ví dụ #1 strcmp () ví dụstrcmp() example

$a = "what go around, come around";
$b = "what goes around, comes around";
8

Xem thêm

  • strcasecmp () - Chuỗi phân tích không phân biệt trường hợp an toàn nhị phân
  • preg_match () - Thực hiện phù hợp với biểu thức thông thường
  • SPERTR_COMPARE () - So sánh an toàn nhị phân của hai chuỗi từ phần bù, lên đến các ký tự dài
  • strncmp () - So sánh chuỗi an toàn nhị phân của n ký tự đầu tiên
  • strstr () - Tìm lần xuất hiện đầu tiên của chuỗi
  • subtr () - trả về phần của chuỗi

jendoj tại gmail dot com ¶

10 năm trước

$a = "what go around, come around";
$b = "what goes around, comes around";
9

$a = "what go around, come around";
$b = "what goes around, comes around";
echo implode(',',array_diff(preg_split('/\h/',$b),preg_split('/\h/',$a)));
0

$a = "what go around, come around";
$b = "what goes around, comes around";
echo implode(',',array_diff(preg_split('/\h/',$b),preg_split('/\h/',$a)));
1

lehal2 tại hotmail dot com

9 năm trước

$a = "what go around, come around";
$b = "what goes around, comes around";
echo implode(',',array_diff(preg_split('/\h/',$b),preg_split('/\h/',$a)));
2

$a = "what go around, come around";
$b = "what goes around, comes around";
echo implode(',',array_diff(preg_split('/\h/',$b),preg_split('/\h/',$a)));
3

$a = "what go around, come around";
$b = "what goes around, comes around";
echo implode(',',array_diff(preg_split('/\h/',$b),preg_split('/\h/',$a)));
1

Rob Wiesler ¶

13 năm trước

$a = "what go around, come around";
$b = "what goes around, comes around";
echo implode(',',array_diff(preg_split('/\h/',$b),preg_split('/\h/',$a)));
5

$a = "what go around, come around";
$b = "what goes around, comes around";
echo implode(',',array_diff(preg_split('/\h/',$b),preg_split('/\h/',$a)));
6

$a = "what go around, come around";
$b = "what goes around, comes around";
echo implode(',',array_diff(preg_split('/\h/',$b),preg_split('/\h/',$a)));
7

$a = "what go around, come around";
$b = "what goes around, comes around";
echo implode(',',array_diff(preg_split('/\h/',$b),preg_split('/\h/',$a)));
8

$a = "what go around, come around";
$b = "what goes around, comes around";
echo implode(',',array_diff(preg_split('/\h/',$b),preg_split('/\h/',$a)));
9

mikael1 tại mail dot ru ¶

3 năm trước

goes,comes
0

goes,comes
1

$a = "what go around, come around";
$b = "what goes around, comes around";
echo implode(',',array_diff(preg_split('/\h/',$b),preg_split('/\h/',$a)));
8

goes,comes
3

Hrodicus tại gmail dot com

11 năm trước

goes,comes
4

goes,comes
5

goes,comes
6

$a = "what go around, come around";
$b = "what goes around, comes around";
echo implode(',',array_diff(preg_split('/\h/',$b),preg_split('/\h/',$a)));
1

luizvid tại gmail dot com ¶

7 năm trước

goes,comes
8

goes,comes
9

$a = "what go around, come around";
$b = "what goes around, comes around";
echo implode(',',array_diff(preg_split('/\h/',$b),preg_split('/\h/',$a)));
1

jcanals tại totsoft dot com ¶

18 năm trước

array array_diff ( array $array1 , array $array2 [, array $... ] )
1

array array_diff ( array $array1 , array $array2 [, array $... ] )
2

$a = "what go around, come around";
$b = "what goes around, comes around";
echo implode(',',array_diff(preg_split('/\h/',$b),preg_split('/\h/',$a)));
1

Chris tại Unix-Ninja Dot Com ¶

9 năm trước

array array_diff ( array $array1 , array $array2 [, array $... ] )
4

array array_diff ( array $array1 , array $array2 [, array $... ] )
5

array array_diff ( array $array1 , array $array2 [, array $... ] )
6

array array_diff ( array $array1 , array $array2 [, array $... ] )
7

array array_diff ( array $array1 , array $array2 [, array $... ] )
8

array array_diff ( array $array1 , array $array2 [, array $... ] )
9

$a0

$a = "what go around, come around";
$b = "what goes around, comes around";
echo implode(',',array_diff(preg_split('/\h/',$b),preg_split('/\h/',$a)));
1

Rob Wiesler ¶

13 năm trước

$a2

mikael1 tại mail dot ru ¶

13 năm trước

$a3

mikael1 tại mail dot ru ¶

3 năm trước

$a4

$a5

$a = "what go around, come around";
$b = "what goes around, comes around";
echo implode(',',array_diff(preg_split('/\h/',$b),preg_split('/\h/',$a)));
1

Hrodicus tại gmail dot com

11 năm trước

$a7

$a8

$a = "what go around, come around";
$b = "what goes around, comes around";
echo implode(',',array_diff(preg_split('/\h/',$b),preg_split('/\h/',$a)));
1