PHP 的 strstr() 等价于 JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26782213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:28:26  来源:igfitidea点击:

PHP's strstr() equivalent for JavaScript

javascriptphp

提问by Daniel_V

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.

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

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

采纳答案by Mark Zucchini

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

JavaScript String 的indexOf()功能应该可以满足你的所有要求

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

下一个示例在 JavaScript 中所做的几乎与您在 PHP 中编写的相同。请记住,您需要确保有一种方法可以传递$_POST['theaddress']给您的 this 函数。(例如使用 PHPsetcookie()函数然后读取 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;
}

回答by Magicprog.fr

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(‘[email protected]', ‘@');
    // *     returns 3: ‘@example.com'
    // *     example 4: strstr(‘[email protected]', ‘@', 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/

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

回答by msfoster

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

回答by Marc Guiselin

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

this will output 10

这将输出 10

回答by motanelu

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/

这里的例子http://jsfiddle.net/bsr3jcuw/

回答by Nielsen Martins Gon?alves

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:

示例 1:

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

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

returns: 'van Zonneveld'.

返回:'van Zonneveld'。

Example2:

示例2:

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

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

returns: 'Kevin '

返回:'凯文'

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

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