最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PHP equivalent of indexOf - Stack Overflow

programmeradmin0浏览0评论

I have the following 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.

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?

I have the following 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.

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?

Share Improve this question edited Jul 4, 2012 at 14:10 Cameron asked Jul 4, 2012 at 14:00 CameronCameron 28.8k102 gold badges288 silver badges490 bronze badges 6
  • 8 As below strpos() is the answer... but you would have found this yourself with a 2 second search! – Brian Commented Jul 4, 2012 at 14:04
  • would have been easier to write this as if( /(search|list|view|contact)\.php/.test(window.location.href)) though – Esailija Commented Jul 4, 2012 at 14:05
  • I don't understand how to achieve the above with strpos – Cameron Commented Jul 4, 2012 at 14:14
  • There are some fairly simple and straight forward examples right on the php page. just play around a bit, i'm sure you will learn 100x more than if we just give you the answer. – Delusional Logic Commented Jul 4, 2012 at 14:14
  • Considering NONE of the examples deal with the URL of the page... – Cameron Commented Jul 4, 2012 at 14:16
 |  Show 1 more comment

5 Answers 5

Reset to default 5

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

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

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

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

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

In my opinion, PHP switch statement is better than if statements in combination with PHP strpos() function.

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

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;
}

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

JS version:

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.

PHP version:

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.

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

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.

strpos();

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

发布评论

评论列表(0)

  1. 暂无评论