4

I wrote this little piece of code for viewing the profile pictures that are set as private. But every time I have to access it, I have to turn Apache on on XAMPP. Also, it is pretty much useless on a computer that doesn't have Apache installed. So I wanted to write it in JavaScript but couldn't find an equivalent for strstr() function.

Could someone please let me know if there's an equivalent or alternative?

The code:

<?php
    //haystack
    $_POST['theaddress'] ='160x160/photo.jpg';

    //return all after the needle '160x160/', inclusive
    $varA = strstr($_POST['theaddress'], '160x160/');

   //build new URL
    $varA = "https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-ash3/t1.0-9"."$varA";

    header("Location: $varA");
?>
Christian
  • 332
  • 1
  • 7
  • 19
Daniel_V
  • 174
  • 1
  • 2
  • 16

6 Answers6

3

You can try this :

function strstr(haystack, needle, bool) {
    // Finds first occurrence of a string within another
    //
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/strstr    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: strstr(‘Kevin van Zonneveld’, ‘van’);
    // *     returns 1: ‘van Zonneveld’    // *     example 2: strstr(‘Kevin van Zonneveld’, ‘van’, true);
    // *     returns 2: ‘Kevin ‘
    // *     example 3: strstr(‘name@example.com’, ‘@’);
    // *     returns 3: ‘@example.com’
    // *     example 4: strstr(‘name@example.com’, ‘@’, true);    // *     returns 4: ‘name’
    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);
        }
    }
}

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

Magicprog.fr
  • 4,072
  • 4
  • 26
  • 35
3
var string='aadsasdsad160x160/saddas'
    console.log(string.indexOf('160x160/'))

this will output 10

Marc Guiselin
  • 3,442
  • 2
  • 25
  • 37
2

JavaScript String's indexOf() function should meet all your requests

Next example does in JavaScript almost same what you wrote in PHP. Keep in mind that you need to be sure that you have a way to pass $_POST['theaddress'] to your this function. (for example use PHP setcookie() function and then read value in JavaScript code

function reload(fileName) {
    // define server address variable
    var serverAddress = "https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-ash3/t1.0-9";

    // get the filename
    var fileName = fileName.indexOf("160x160/");

    // set new location based on server address and filename
    location.href = serverAddress + fileName;
}
Mark Zucchini
  • 925
  • 6
  • 11
  • 1
    http://jsfiddle.net/danielvictorg/daog571x/ Pretty ugly code but it works. Used most of your code. Thanks! Thought there would be a more obvious function than `indexOf()` as it returns the position of the string. – Daniel_V Nov 06 '14 at 16:22
1

As usual a solution is already provided when I get back..

function strstr(haystack, needle, before_needle) {
    if(haystack.indexOf(needle) >= 0) 
        return before_needle ? haystack.substr(0, haystack.indexOf(needle)) 
               : haystack.substr(haystack.indexOf(needle));
    return false;
}
msfoster
  • 2,474
  • 1
  • 17
  • 19
0

Try this

String.prototype.strstr = function(search) {
    var position = this.indexOf(search);
    if (position == -1) {
        // not found
        return false;
    }

    return this.slice(position);
};

var x = 'Hello cruel world';
alert(x.strstr('cruel'));

Example here http://jsfiddle.net/bsr3jcuw/

motanelu
  • 3,945
  • 1
  • 14
  • 21
0

You can create a function like this:

function strstr(haystack, needle, bool) {  

    var pos = 0;

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

Example1:

stristr('Kevin van Zonneveld', 'Van');

returns: 'van Zonneveld'.

Example2:

stristr('Kevin van Zonneveld', 'VAN', true);

returns: 'Kevin '

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