How to add characters to the beginning of a string in php

青灯夜游
Release: 2023-03-16 17:36:01
Original
2907 people have browsed it

Two implementation methods: 1. Use the string connector "." to splice the specified character at the beginning of the string, and the syntax is "specified character. string"; 2. Use the substr_replace() function to add the specified character to the beginning of the string. To insert the specified character at the beginning of the string, you only need to set the second parameter of the function to the specified character, and set the third and fourth parameters to 0. The syntax is "substr_replace(string, specified character, 0, 0)" .

How to add characters to the beginning of a string in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

In PHP, you can use string concatenation or substr_replace() inserts to add characters to the beginning of the string.

Method 1: Use the string connector "." to splice

String connector . can combine two or more strings are concatenated into a new string.

You only need to use the string connector "." to splice the specified characters at the beginning of the original string

<?php
header("Content-type:text/html;charset=utf-8");
$str1="hello";
echo "原字符串:".$str1."<br>";
$ch="A";
echo "指定字符:".$ch."<br>";
$str=$ch.$str1;
echo "在字符串首部增加指定字符后:".$str;
?>
Copy after login

How to add characters to the beginning of a string in php

Method 2: Use The substr_replace() function performs insertion

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

substr_replace(string,replacement,start,length)
Copy after login
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
  • Return value: Returns the replaced string.

#You only need to set the second parameter of the function to the specified character, the third parameter to 0, and the fourth parameter to 0.

<?php
header("Content-type:text/html;charset=utf-8");
$str1="hello";
echo "原字符串:".$str1."<br>";
$ch="W";
echo "指定字符:".$ch."<br>";
$str=substr_replace($str1,$ch,0,0);
echo "在字符串首部增加指定字符后:".$str;
?>
Copy after login

How to add characters to the beginning of a string in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to add characters to the beginning 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