Cameron Cameron - 1 year ago 173
Javascript Question

PHP equivalent of indexOf

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?

Answer Source

Although your JavaScript code is using indexOf, PHP actually has a better way of handling what you need to do. Your simply checking the script name within your URL and then performing some code, and as such this PHP code is probably better than various IF statements using strpos()

switch(basename($_SERVER['SCRIPT_NAME'])) {
  case 'search.php'
  case 'list.php':
  case 'view.php':
  case 'contact.php':
    .... perform code on anything except the above cases ....
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download