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 | Show 1 more comment5 Answers
Reset to default 5The 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).
if( /(search|list|view|contact)\.php/.test(window.location.href))
though – Esailija Commented Jul 4, 2012 at 14:05