PHP 输出单词不在字符串中

发布于 2024-09-12 15:47:06 字数 341 浏览 0 评论 0 原文

我有两个字符串:

$string = shell_exec('reg.bat '.$arg); //returns word\0word\0word
$stringA = "$string";
$stringB = "word,other,word2,other2";
$array1 = explode('\0', $stringA);
$array2 = explode(',', $stringB);
$result = array_diff($array1, $array2);

我可以使用 array_diff 来查找差异,但最后一个单词显示为不在两个字符串中,即使这是因为 \0 分隔符。

I have two strings:

$string = shell_exec('reg.bat '.$arg); //returns word\0word\0word
$stringA = "$string";
$stringB = "word,other,word2,other2";
$array1 = explode('\0', $stringA);
$array2 = explode(',', $stringB);
$result = array_diff($array1, $array2);

I can use array_diff to find the differences, but the last word shows up as not in both strings even though it is because of the \0 delimiter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浸婚纱 2024-09-19 15:47:06

尝试:

$stringA = "word,other,word2,other";
$stringB = "word,other,word2,other2";

$array1 = explode(',', $stringA);
$array2 = explode(',', $stringB);

$result = array_diff($array2, $array1);

echo '<pre>';
print_r($result);

结果:

Array
(
    [3] => other2
)

更多信息:

Try:

$stringA = "word,other,word2,other";
$stringB = "word,other,word2,other2";

$array1 = explode(',', $stringA);
$array2 = explode(',', $stringB);

$result = array_diff($array2, $array1);

echo '<pre>';
print_r($result);

Result:

Array
(
    [3] => other2
)

More Info:

随风而去 2024-09-19 15:47:06

将字符串分解为数组并查看 array_diff 功能。

Explode the string to an array and have a look at the array_diff function.

无可置疑 2024-09-19 15:47:06

您可以使用 array_diff 和 str_word_count:

print_r(
    array_diff(
        str_word_count("word\0word\0word", 1, '0123456789'),
        str_word_count("word,other,word2,other2", 1, '0123456789'))
);
// will return an empty array, because "word" is in "word,other,word2,other2"

或者切换输入:

print_r(
    array_diff(
        str_word_count("word,other,word2,other2", 1, '0123456789'),
        str_word_count("word\0word\0word", 1, '0123456789'))
);
// will return an array containing "other", "word2", "other"

You can use array_diff and str_word_count:

print_r(
    array_diff(
        str_word_count("word\0word\0word", 1, '0123456789'),
        str_word_count("word,other,word2,other2", 1, '0123456789'))
);
// will return an empty array, because "word" is in "word,other,word2,other2"

Or switch the input around:

print_r(
    array_diff(
        str_word_count("word,other,word2,other2", 1, '0123456789'),
        str_word_count("word\0word\0word", 1, '0123456789'))
);
// will return an array containing "other", "word2", "other"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文