简体   繁体   中英

String replace only first character in Laravel View

I'm using Laravel for my website.

In my view, I received a string variable from controller like this "01230456", I want to replace only first character "0" to "ABC", not the second "0" in string.

I tried preg_replace('\\0\\', 'ABC', $string, 1) , but it replaced all "0" character in my string.

How should I fix this?

Thank you!

Have you tried this

preg_replace('/0/', 'ABC', $string, 1)

or you can use substr

$sub = substr($string, 1);
$new = 'ABC' . $sub;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM