PHP substr_replace Replacement string Specify position character

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags echo php php tutorial programming replace replaced replacement set to

PHP substr_replace Replacement string Specify position character
/*
int Mb_strlen (String $str [, String $encoding])

<?php Tutorial


Echo substr_replace ("Hello World", "Earth", 6); Hello Earth


?>

It is worth mentioning that when the start and length are both negative and the length is less than or equal to the beginning, the length will have an effect set to 0.

<?php


substr_replace (' Eggs ', ' x ', -1,-1); Eggxs


substr_replace (' Eggs ', ' x ', -1,-2); Eggxs


substr_replace (' Eggs ', ' x ', -1,-2); Eggxs


?>

Mahouve as:
<?php
Substr_replace (' eggs ', ' x ', -1,0); Eggxs
?>

&lt;?php


substr_replace (' Huevos ', ' x ', -2,-2); Huevxos


substr_replace (' Huevos ', ' x ', -2,-3); Huevxos


substr_replace (' Huevos ', ' x ', -2,-3); Huevxos


?&gt;

Mahouve as:

&lt;?php


substr_replace (' Huevos ', ' x ', -2,0); Huevxos


?&gt;

Another note that if the length is negative and the starting offset is the same length position, the length (again) will have an effect that is set to 0. (Of course, mentioned in the manual, when the length is negative it actually represents the previous position)

<?php
Substr_replace (' ABCD ', ' X ', 0,-4); Xabcd
?>

Mahouve as:
<?php
Substr_replace (' ABCD ', ' X ', 0,0); Xabcd
?>

<?php
Substr_replace (' ABCD ', ' X ', 1,-3); Axbcd
?>

Mahouve as:
<?php
Substr_replace (' ABCD ', ' X ', 1, 0); Axbcd
?>

Look at the parameter description


String required. Specify the string to check.
Replacement required. Specify the string to insert.
Start Required. Specify where the string begins to be replaced.

Positive number-replace at start offset
Negative number-replaces the start offset at the end of the string
0-Start the substitution at the first character in the string

Charlist Optional. Specify how many characters to replace.

Positive number-The length of the string being replaced
Negative numbers-the number of replaced characters starting at the end of the string
0-Insert rather than replace

Related Article

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.