javascript PHP 相当于 indexOf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11330582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:48:29  来源:igfitidea点击:

PHP equivalent of indexOf

phpjavascript

提问by Cameron

I have the following JS:

我有以下JS:

if(window.location.href.indexOf("search.php") != -1 
    || window.location.href.indexOf("list.php") != -1
    || window.location.href.indexOf("view.php") != -1
    || window.location.href.indexOf("contact.php") != -1) {

But want to convert it to PHP. What is the equivalent of indexOf in PHP or the best way to do this in PHP.

但是想把它转换成PHP。什么是 PHP 中 indexOf 的等价物或在 PHP 中执行此操作的最佳方法。

I don't understand the strpos examples people are linking to below. Perhaps an example more in line with what I have done in JS?

我不明白人们在下面链接的 strpos 示例。也许一个更符合我在 JS 中所做的例子?

回答by de Raad

The question asks for an equivalent. PHP's strpos() does not work on an array, whereas javascript's indexOf does (by treating a string as an array automatically). So here's a PHP equivalent

该问题要求等效。PHP 的 strpos() 不适用于数组,而 javascript 的 indexOf 可以(通过自动将字符串视为数组)。所以这是一个 PHP 等价物

function indexOf($array, $word) {
    foreach($array as $key => $value) if($value === $word) return $key;
    return -1;
}

// Example: indexOf(['hi','bye'], 'bye') returns 1

回答by nickb

strpos()should do the trick, also it returns boolean falseon failure.

strpos()应该可以解决问题,它也会false在失败时返回布尔值。

JS version:

JS版本:

The indexOf() method returns the position of the first occurrence of a specified value in a string. This method returns -1 if the value to search for never occurs. Note: The indexOf() method is case sensitive.

indexOf() 方法返回指定值在字符串中第一次出现的位置。如果从未出现要搜索的值,则此方法返回 -1。注意: indexOf() 方法区分大小写。

PHP version:

PHP版本:

int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

Find the numeric position of the first occurrence of needle in the haystack string. Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1. Returns FALSE if the needle was not found.

在 haystack 字符串中找到第一个出现的针的数字位置。返回针相对于 haystack 字符串开头的位置(与偏移量无关)。另请注意,字符串位置从 0 开始,而不是 1。如果未找到针,则返回 FALSE。

回答by Rawkode

Although your JavaScript code is using indexOf(), PHP actually has a better way of handling what you want to achieve.

尽管您的 JavaScript 代码使用的是indexOf(),但 PHP 实际上有更好的方式来处理您想要实现的目标。

You simply have to check the script name in your URL, then perform an action(s) based on it.

您只需检查 URL 中的脚本名称,然后根据它执行操作。

In my opinion, PHP switchstatement is better than ifstatements in combination with PHP strpos()function.

在我看来,PHPswitch语句比if语句结合 PHPstrpos()函数要好。

Below is an illustration of how you can achieve that using a PHP switch statement:

下面是如何使用 PHP switch 语句实现这一目标的说明:

switch(basename($_SERVER['SCRIPT_NAME'])) {
  case 'search.php':
    // Script to run for search.php
    break;
  case 'list.php':
    // Script to run for list.php
    break;
  case 'view.php':
    // Script to run for view.php
    break;
  case 'contact.php':
    break;
  default:
      // Perform code on anything except the above cases.
    break;
}

回答by Brett Zamir

In reply to your secondary question, strpos could be used as follows:

在回答您的次要问题时,可以按如下方式使用 strpos:

if (strpos($_SERVER['SCRIPT_NAME'], "search.php") !== false
    || strpos($_SERVER['SCRIPT_NAME'], "list.php") !== false
    || strpos($_SERVER['SCRIPT_NAME'], "view.php") !== false
    || strpos($_SERVER['SCRIPT_NAME'], "contact.php") !== false) {

...though indeed Rawkode's answer is more elegant.

...虽然确实 Rawkode 的回答更优雅。

回答by niconoe

strpos();

strpos();

You should use mb_strpos() if you're not using ISO-8859-1 encoding (UTF-8 or others).

如果您不使用 ISO-8859-1 编码(UTF-8 或其他),则应使用 mb_strpos()。