Skip to main content

Posts

Showing posts with the label String

Replace the last occurrence of a string in a string using PHP

Code to replace the last occurrence of a string in a string using PHP <?php function str_lreplace ( $search , $replace , $subject ) { $pos = strrpos ( $subject , $search ); print $pos ; if ( $pos !== false ) { $subject = substr_replace( $subject , $replace , $pos , strlen ( $search )); } echo $subject ; return $subject ; } str_lreplace( 'hello' , 'world' , 'this is hello world for checking hello when hello' ); ?> output :

Replace the last occurrence of an expression in a string

Code to  replace the last occurrence of an expression or string in another string using python. def rreplace (s, old, new, occurrence): li = s . rsplit(old, occurrence) print new . join(li) return new . join(li) rreplace( '123434352232' , '2' , '6' , 1 ) Output :