php 只替换第一次出现的字符串
例
$str='这是字符串我只替换ABC一次后面的ABC我不替换了,有没有办法实现。';
把第一个abc替换成xyz,由于要替换的字符串是固定的,很多人想到了用str_replace()函数,看看这个函数的使用是不是我们要的
str_replace( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
不小心还真以为是我们想要的呢,最后那个参数是返回替换发生的总次数,它是一个引用变量,而不是我要想要的指定它将替换几次,所以用str_replace()是不行的
preg_replace()是可以实现的,可惜用了正则,
代码如下 | 复制代码 |
$str=preg_replace('/abc/','abc',$str,1); |
例
显示email为 从@前2位(含)开始向前隐藏4位
代码如下 | 复制代码 |
function show_email_2($string){ 有没有不用正则的,嗯可以这样 $replace='xyz'; |
如果我想替换到指定次数可参考下面方法
代码如下 | 复制代码 |
/* } return rtrim($result_str,$word); |
实例都很好理解,如果你不想深入了解我们直接使用str_replace即可实例了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The explode function in PHP is a function used to split a string into an array. It is very common and flexible. In the process of using the explode function, we often encounter some errors and problems. This article will introduce the basic usage of the explode function and provide some methods to solve the error reports. 1. Basic usage of the explode function In PHP, the basic syntax of the explode function is as follows: explode(string$separator,string$stri

In PHP programming, processing strings is a frequently required operation. Among them, splitting and merging strings are two common requirements. In order to perform these operations more conveniently, PHP provides two very practical functions, namely the explode and implode functions. This article will introduce the usage of these two functions, as well as some practical skills. 1. The explode function The explode function is used to split a string according to the specified delimiter and return an array. Its function prototype is as follows: arra

This article will explain in detail the ASCII value of the first character of the string returned by PHP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP returns the ASCII value of the first character of a string Introduction In PHP, getting the ASCII value of the first character of a string is a common operation that involves basic knowledge of string processing and character encoding. ASCII values are used to represent the numeric value of characters in computer systems and are critical for character comparison, data transmission and storage. The process of getting the ASCII value of the first character of a string involves the following steps: Get String: Determine the string for which you want to get the ASCII value. It can be a variable or a string constant

Title: Common errors and solutions when using the explode function in PHP In PHP, the explode function is a common function used to split a string into an array. However, some common errors can occur due to improper use or incorrect data format. This article will analyze the problems you may encounter when using the explode function, and provide solutions and specific code examples. Mistake 1: The delimiter parameter is not passed in. When using the explode function, one of the most common mistakes is that the delimiter parameter is not passed in.

This article will explain in detail how PHP returns the string from the start position to the end position of a string in another string. The editor thinks it is quite practical, so I share it with you as a reference. I hope you will finish reading this article. You can gain something from this article. Use the substr() function in PHP to extract substrings from a string. The substr() function can extract characters within a specified range from a string. The syntax is as follows: substr(string,start,length) where: string: the original string from which the substring is to be extracted. start: The index of the starting position of the substring (starting from 0). length (optional): The length of the substring. If not specified, then

Understand the substr() function in PHP for intercepting strings. In the PHP language, the substr() function is a very useful string processing function, which can be used to intercept string fragments at a specified position and length. The substr() function accepts three parameters: the string to be intercepted, the starting position of the interception, and the length of the interception. Below we will introduce the use of the substr() function in detail and give specific code examples. Basic usage of substr() function substr() function

Use the PHP function "explode" to split a string into an array. In PHP development, you often encounter situations where you need to split a string according to the specified delimiter. At this time, we can use PHP's built-in function "explode" to convert string to array. This article will introduce how to use the "explode" function to split a string and give relevant code examples. The basic syntax of the "explode" function is as follows: arrayexplode(

Use the PHP function "substr" to obtain the substring of a string. In PHP programming, you often encounter situations where you need to obtain part of the content of a string. At this time, we can use PHP's built-in function "substr" to achieve this. This article explains how to use the "substr" function to get a substring of a string and provides some code examples. 1. Basic usage of the substr function The substr function is used to obtain a substring of a specified length from a string. Its basic syntax is as follows: substr(
