PHP strstr()等效于JavaScript

时间:2014-11-06 14:44:29

标签: javascript php

我写了这段小代码,用于查看设置为私有的个人资料图片。但每次我必须访问它,我必须在XAMPP上打开Apache。而且,在没有安装Apache的计算机上它几乎没用。所以我想用JavaScript编写它,但找不到strstr()函数的等价物。

有人可以告诉我是否有等同或替代?

代码:

<?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");
?>

6 个答案:

答案 0 :(得分:2)

你可以试试这个:

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

来源:http://phpjs.org/functions/strstr/

答案 1 :(得分:2)

JavaScript字符串的indexOf()功能应该满足您的所有要求

下一个示例在JavaScript中与您在PHP中编写的内容几乎相同。请注意,您需要确保有办法将$_POST['theaddress']传递给此功能。 (例如,使用PHP setcookie()函数,然后在JavaScript代码中读取值

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

答案 2 :(得分:0)

var string='aadsasdsad160x160/saddas'
    console.log(string.indexOf('160x160/'))

这将输出10

答案 3 :(得分:0)

试试这个

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'));

此处示例http://jsfiddle.net/bsr3jcuw/

答案 4 :(得分:0)

您可以创建如下函数:

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

<强>例1:

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

返回:'van Zonneveld'。

<强>例2:

stristr('Kevin van Zonneveld','VAN',是的);

返回:'凯文'

参考:http://phpjs.org/functions/stristr/

答案 5 :(得分:0)

像往常一样,当我回来时已经提供了解决方案..

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