1

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?

6
  • 8
    As below strpos() is the answer... but you would have found this yourself with a 2 second search! 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 Commented Jul 4, 2012 at 14:05
  • I don't understand how to achieve the above with strpos 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. Commented Jul 4, 2012 at 14:14
  • Considering NONE of the examples deal with the URL of the page... Commented Jul 4, 2012 at 14:16

5 Answers 5

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 
Sign up to request clarification or add additional context in comments.

Comments

4

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

4 Comments

Can I combine the URLs together? As the ... perform code ... is the same for all matches.
You can remove the breaks to allow all case statements to perform same code. I shall update
Yeah but if it matches those then I don't want the code to execute, so it's ANYTHING but those match the url.
Still not working! $_SERVER['SCRIPT_NAME'] returns the prefix too, so if I have domain.com/candidates/search.php` it won't match up. Any ideas on how to match it?
3

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.

Comments

2

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.

Comments

1

strpos();

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.