String processing-PHP character replacement

Source: Internet
Author: User
Directly run the code {code...}. Shouldn't the result be 0Handler1TestHandler. php? Is there a problem? Run the Code directly.

$str = '\Handler\TestHandler.php';$need = ['\\', '\\'];$new_need = [0,1];$result = str_replace($need, $new_need, $str);//$result=>0Handler0TestHandler.php

The result should not be0Handler1TestHandler.php?

Is there a problem?

Reply content:

Run the Code directly.

$str = '\Handler\TestHandler.php';$need = ['\\', '\\'];$new_need = [0,1];$result = str_replace($need, $new_need, $str);//$result=>0Handler0TestHandler.php

The result should not be0Handler1TestHandler.php?

Is there a problem?

Your two\All are replaced by 0. Syntax details

Php should not have this built-in method. You need to write a function by yourself. The following should be able to implement your function.

function replace ($str) {    $i = 0;    $result = '';    $arr = explode("\\", $str);    foreach ($arr as $val) {        if ($val === '') continue;        $result .= ($i++).$val;    }    return $result;}

Because array replacement is a successive replacement, the first replacement has already replaced two\If it is replaced with 0, the replacement will not match.\No.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.