是什么原因导致 substr 失败?

发布于 2024-11-05 23:43:06 字数 349 浏览 5 评论 0原文

简单的 substr 调用无法正常运行。我只想抓取以正斜杠结尾的字符串。这里有七个字符串。

HELLO/NN, SMILE/JJ, JUMP/, GOOD/RB, GREAT/JJ, HAPPY/NNP, SEAPORT/


$m = substr($string, -1);

 if ($m = "/") {
     echo $string;
 }

这段代码每次都会以某种方式返回 true。七个字全部打印出来。 我尝试过 strrev 和许多其他字符串函数。似乎没有 事情。我可以从字面上打印 $m 并看到它是“/”,但是 if 语句 确定每个单词都满足 $m = "/" 标准。即使 $m 不是“/”

A simple substr call is not functioning properly. I want to grab only the strings which end with a forward slash. Here are seven strings.

HELLO/NN, SMILE/JJ, JUMP/, GOOD/RB, GREAT/JJ, HAPPY/NNP, SEAPORT/


$m = substr($string, -1);

 if ($m = "/") {
     echo $string;
 }

This code somehow returns true every time. All seven words are printed.
I've tried strrev and many other string functions. It doesn't seem to
matter. I can literally print $m and see that it's "/" but the if statement
decides that every word meets the $m = "/" criteria. Even when $m is not a "/"

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

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

发布评论

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

评论(3

一萌ing 2024-11-12 23:43:06

比较运算符是 ==,而不是 =

if ($m == "/") {
     echo $string;
}

The comparison operator is ==, not =:

if ($m == "/") {
     echo $string;
}
月下伊人醉 2024-11-12 23:43:06

难道不应该是 == 而不是 = 吗?

Shouldn't that be == rather than =?

神仙妹妹 2024-11-12 23:43:06
if ($m = "/") {

您将值“/”分配给 $m 并且该评估返回 true。您想要比较并且应该使用

if ($m == "/") {
if ($m = "/") {

You are assigning the value "/" to $m and that evaluation returns true. You want to compare and should use

if ($m == "/") {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文