例
$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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!
