简体   繁体   English

javascript或PHP执行搜索和替换功能

[英]javascript or PHP to perform search and replace functionality

i wanted to ask this for a while. 我想问一会儿。 what is the difference in terms of efficiency for using javascript or PHP in performing a regular expression search and replace functionality? 使用JavaScript或PHP执行正则表达式搜索和替换功能的效率有何区别?

if i were to develop a website, both language can perform the same function but im just curious what normally professional web developers use. 如果我要开发一个网站,那么两种语言都可以执行相同的功能,但我只是好奇通常专业的Web开发人员使用的是什么。

i want to know the standards when to use javascript or PHP if both can perform the same. 我想知道何时使用javascript或PHP的标准(如果两者都可以执行相同的操作)。

Javascript var result = str.replace(); JavaScript var result = str.replace();

PHP preg_replace(); PHP preg_replace();

It really isn't a matter of performance in this case, but rather, where you want to do the replacing. 这真的不是在这种情况下的性能问题,而是你想要做的更换。 If you want to do something in the client-side, you use JavaScript. 如果要在客户端执行某些操作,请使用JavaScript。 If you want to do it in the server-side, you use PHP. 如果要在服务器端执行此操作,请使用PHP。

Also: If you can get to choose (say, instead of doing the replace in PHP and then echoing it, just replacing in the client-side with JavaScript), I'd use JavaScript because the users' browsers do the job instead of your server, and therefore it would reduce the server load. 另外:如果您可以选择(例如,而不是在PHP中进行替换然后回显它,而只是在客户端中用JavaScript替换),我将使用JavaScript,因为用户的浏览器代替了您的服务器,因此将减少服务器负载。 But this would only matter if you had thousands of replaces per minute or something like that. 但这仅在您每分钟有数千次更换或类似操作时才重要。

More than likely the answer is going to depend on more than the raw performance of it. 答案很可能将不仅取决于原始性能。 For example, let us assume that if you are using PHP in combination with Javascript, you likely mean to say that Javascript is happening on the browser side, and PHP on the server side. 例如,让我们假设,如果您将PHP与Javascript结合使用,则可能意味着Javascript在浏览器端发生,而PHP在服务器端发生。

If you need to validate the format of a phone number for example, you could do that both before the form is submitted (in Javascript inside the browser) or server side in PHP. 例如,如果您需要验证电话号码的格式,则可以在提交表单之前(在浏览器中使用Javascript)或在PHP的服务器端进行验证。

Let's say you did it in Javascript, now what happens when a user without Javascript comes by and fills out your form? 假设您是用Java脚本完成的,那么当没有Java脚本的用户过来填写您的表单时会发生什么呢? Or more likely, a spam bot? 或更可能是垃圾邮件机器人? Incorrectly formatted data is now being passed back to your server and might trigger errors or other unexpected conditions. 现在,格式错误的数据将被传递回您的服务器,并可能触发错误或其他意外情况。 In this example you will likely want to validate the phone number on the server side at the very least. 在此示例中,您可能至少要在服务器端验证电话号码。 But you might want to be able to tell the user that the phone number is invalid before submitting without having to wait for the server to validate it and send back errors, so doing it in Javascript on the browser side in addition to PHP on the server side might be the way you choose to implement it. 但是您可能希望能够在提交之前告诉用户电话号码无效,而不必等待服务器验证它并发送回错误,因此除了服务器上的PHP 之外,还可以使用浏览器端的Javascript进行操作方面可能是您选择实施的方式。

I hope this helps to demonstrate some of the considerations of where to implement your logic. 我希望这有助于说明在何处实现逻辑的一些注意事项。

A very unreliable bench mark shows that the browser may actually be faster. 一个非常不可靠的基准表明浏览器实际上可能更快。 Although there may be some very large optimizations going on in the browser using the test case I made versus the PHP case I made. 尽管在浏览器中使用我编写的测试用例和我编写的PHP用例可能会进行一些非常大的优化。

This post also supports the findings: Speed of PHP vs JavaScript? 这篇文章还支持以下发现: PHP与JavaScript的速度?

Running this code in PHP: 在PHP中运行以下代码:

<?php
    require("Logger.php");

    $string = "apples and bananas";

    $logger = new Logger("Start");
    for ($x = 0; $x < 1000000; $x++) {
        $newString = str_replace("bananas", "oranges", $string);
    }
    $logger->log("Finish");

    echo $logger->status();

Came back with 10867ms on my local machine (WAMP). 在本地计算机(WAMP)上以10867ms的速度返回。 (And removing the class I'm using for logging does NOT speed it up.) (并且删除我用于记录的类不会加快它的速度。)

Running the following JS code ( http://jsfiddle.net/MQKLT/3/ ) is returning roughly 343ms (and is visibly faster). 运行下面的JS代码( http://jsfiddle.net/MQKLT/3/ )返回大约343毫秒(并且明显更快)。

function go() {
    var str = "apples and bananas";

    var newString = "";

    var d = new Date();
    var n = (d.getSeconds() * 1000) + d.getMilliseconds();

    for (var x = 0; x < 1000000; x++) {
        newString = str.replace("bananas", "oranges");
        newString = newString + x;
    }

    var d2 = new Date();
    var n2 = (d2 .getSeconds() * 1000) + d2.getMilliseconds();

        //console.log("First: " + n + " Second: " + n2 + " Total: " + (n2 - n));
        alert("First: " + n + " Second: " + n2 + " Total: " + (n2 - n) + " Output: " + newString);

}

As I said, the browser may be doing some major optimizations. 正如我所说,浏览器可能正在做一些重大的优化。 The value I posted above was tested in Chrome. 我在上面发布的值已在Chrome中进行了测试。 I do get similar times in IE and a faster time in Firefox (which suggests to me that optimizations are happening.) 在IE中,我确实得到了类似的时间,而在Firefox中,我得到了更快的时间(这向我暗示着优化正在发生。)

With that in mind: I'm a lazy programmer and I tend to go with what is most convenient for me as long as it doesn't compromise security. 考虑到这一点:我是一个懒惰的程序员,并且我倾向于使用对我来说最方便的东西,只要它不会损害安全性即可。 (IE, string manipulation in regards to validation should be done in PHP / away from client control) It makes absolutely no sense to do an AJAX request for a string replace by itself. (即,关于验证的字符串操作应在PHP中完成,而应远离客户端控制)完全没有必要对字符串进行AJAX请求以替换其自身。 For real world cases it depends on a lot of variables such as the browser involved, the PC involved, the server involved. 对于实际情况,它取决于许多变量,例如涉及的浏览器,涉及的PC和涉及的服务器。

If you are worried about string replace performance, there is likely something wrong with the logic you are using to perform your task. 如果您担心字符串替换的性能,则用于执行任务的逻辑可能有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM