Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace first two characters of a string in php

Tags:

string

php

I am dealing with a problem of time conversion from 12 hr format to 24 hour format.

Is there any single function in php to replace the first two characters of a string?

str_replace can be used only when I know the substring content to be replaced.

like image 763
Poornima Satheesh Avatar asked Aug 01 '26 13:08

Poornima Satheesh


1 Answers

$str_to_replace = '12';

$input_str = 'ab345678';

$output_str = $str_to_replace . substr($input_str, 2);

echo $output_str;

"12345678"

like image 50
Siraj Alam Avatar answered Aug 03 '26 02:08

Siraj Alam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!