Friday, March 14, 2014

PHP "strstr" function in Javascript

If you are looking for PHP's strstr function in Javascript you can use following javascript function,

function strstr(haystack, needle, bool) {
   var pos = 0;

  haystack += '';
  pos = haystack.indexOf(needle);
  if (pos == -1) {
    return false;
  } else {
    if (bool) {
      return haystack.substr(0, pos);
    } else {
      return haystack.slice(pos);
    }
  }
}


Reference : http://phpjs.org/functions/strstr/

No comments:

Post a Comment