0

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!

0

1 Answer 1

2

Have you tried this

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

or you can use substr

$sub = substr($string, 1);
$new = 'ABC' . $sub;
Sign up to request clarification or add additional context in comments.

1 Comment

preg_replace('/0/', 'ABC', $string, 1) is worked for me, thank you very much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.