PHP strpos(),找到回显实例

发布于 2024-09-28 15:02:21 字数 86 浏览 1 评论 0原文

在 PHP 中使用 strpos() 函数,如何回显 strpos() 找到的内容?它找到的所有项目。谢谢

Using the strpos() function in PHP, how can I echo what strpos() has found? All items that it has found. Thanks

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

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

发布评论

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

评论(2

蝶舞 2024-10-05 15:02:21

strpos 查找的是您告诉它查找的内容,因此本质上您是在问:

$needle="foo";
if (strpos($haystack, $needle)!==false)
{
    echo $needle;
}

也许您实际上想做还有别的东西吗?

strpos finds is what you tell it to find, so in essense you're asking this:

$needle="foo";
if (strpos($haystack, $needle)!==false)
{
    echo $needle;
}

Maybe you actually want to do something else?

说好的呢 2024-10-05 15:02:21

strpos() 只返回大海捞针中找到的第一根针。如果你需要找到所有这些,你可以这样做:

$str = "abcabcabc";
$arr = array();
$delChar = 0;

while (($pos = strpos($str, "a")) !== false) {
    $arr[] = $pos + $delChar;
    $delChar += strlen($str) - strlen(substr($str, $pos + 1));
    $str = substr($str, $pos + 1);
}

var_dump($arr);

打印 0、3 和 6。

编辑:我刚刚读了评论,我假设你的意思是函数找到的位置,也许我错了:-p

strpos() only returns the first needle found in the haystack. If you need to find all of them, you can do something like this :

$str = "abcabcabc";
$arr = array();
$delChar = 0;

while (($pos = strpos($str, "a")) !== false) {
    $arr[] = $pos + $delChar;
    $delChar += strlen($str) - strlen(substr($str, $pos + 1));
    $str = substr($str, $pos + 1);
}

var_dump($arr);

which prints 0, 3 and 6.

Edit: I just read the comments, I assumed you meant the positions the function found, maybe I was wrong :-p

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