Home > Backend Development > PHP Problem > How to replace the first character of a string in php

How to replace the first character of a string in php

青灯夜游
Release: 2023-03-16 22:26:02
Original
2876 people have browsed it

In PHP, you can use the substr_replace() function to replace the first character of a string; this function can replace a specified number of characters starting from the specified position in the string. You only need to set the third parameter is "0", the fourth parameter is set to 1 to replace the first character, the syntax is "substr_replace(string, replacement character, 0, 1)".

How to replace the first character of a string in php

The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer

In php, You can use the substr_replace() function to replace the first character of a string.

The substr_replace() function can replace the specified number of characters starting from the specified position in the string.

If you want to replace the first character of the string, you only need to set the third parameter to "0" and the fourth parameter to 1. (The index of characters in the string starts with 0, so the starting position of the first character is 0)

Example:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = &#39;hello world!&#39;;
echo "原字符串:".$str."<br><br>";
$replace = &#39;A&#39;;
echo "替换第一位字符:".substr_replace($str, $replace,0,1)."<br>";
?>
Copy after login

How to replace the first character of a string in php

Description: substr_replace() function

The substr_replace() function replaces part of a string with another string.

substr_replace(string,replacement,start,length)
Copy after login
  • substr_replace() Replaces the substring qualified by the start and optional length parameters with replacement in a copy of string string.

  • If start is a positive number, replacement will start from the start position of string. If start is negative, the replacement will start at the start position from the bottom of string.

  • If the length parameter is set and is a positive number, it represents the length of the replaced substring in string. If set to a negative number, it represents the number of characters from the end of the substring to be replaced from the end of string. If this parameter is not provided, the default is strlen(string) (the length of the string). Of course, if length is 0, then the function of this function is to insert replacement at the start position of string.

ParametersDescription
stringRequired. Specifies the string to check.
replacementRequired. Specifies the string to be inserted.
startRequired. Specifies where in the string to begin replacement.
  • Positive number - starts at the specified position in the string
  • Negative number - starts at the specified position from the end of the string
  • 0 - at the first character in the string Start at
length Optional. Specifies how many characters to replace. The default is the same as the string length.
  • Positive number - the length of the string to be replaced
  • Negative number - the number of characters to be replaced from the end of the string
  • 0 - insert instead of replace

4 ways to use str_replace() function

1. String replacement string

<?php
    //实例一:字符串替换字符串
    $str1 = str_replace("red","black","red green yellow pink purple");
    echo $str1."";    //输出结果为black green yellow pink purple
?>
Copy after login

Output:

How to replace the first character of a string in php

2. String replacement array key value

<?php
    //实例二:字符串替换数组键值
    $arr = array("blue","red","green","yellow");
    $str1 = str_replace("red","pink",$arr,$i);
    var_dump($str1);
?>
Copy after login

Output:

How to replace the first character of a string in php

3 , Array replaces array, mapping replaces

<?php
    //实例三:数组替换数组,映射替换
    $arr1 = array("banana","orange");
    $arr2 = array("pitaya","tomato");
    $con_arr = array("apple","orange","banana","grape");
    $con_rep = str_replace($arr1,$arr2,$con_arr,$count);
    var_dump($con_rep);
?>
Copy after login

Output:

How to replace the first character of a string in php

##4. If $search is an array and $replace is a string


<?php
//实例四:如$search为数组,$replace为字符串时
$search = array("banana","grape");
$replace = "tomato";
$arr = array("banana","apple","orange","grape");
$new_arr = str_replace($search,$replace,$arr,$count);
var_dump($new_arr);
?>
Copy after login
Output:

How to replace the first character of a string in php

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to replace the first character of a string in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template