How PHP Replace last occurrence of a String in a String?

0 votes
Anyone know of a very fast way to replace the last occurrence of a string with another string in a string?
Aug 14 in PHP by kartik
• 27,140 points
53 views

1 answer to this question.

0 votes

Hello @kartik,

You can use this function:

function str_lreplace($search, $replace, $subject)
{
    $pos = strrpos($subject, $search);

    if($pos !== false)
    {
        $subject = substr_replace($subject, $replace, $pos, strlen($search));
    }

    return $subject;
}

Hope it helps!!
ThanK you!!

answered Aug 14 by Niroj
• 58,500 points

Related Questions In PHP

0 votes
1 answer

How to merge two arrays while keeping keys instead of reindexing in php?

Hello, Considering that you have $replaced = array('1' => ...READ MORE

answered Apr 1 in PHP by Niroj
• 58,500 points
87 views
0 votes
1 answer

How can I handle the warning of file_get_contents() function in PHP?

Hello @kartik, This is fairly simple: if (!$data = ...READ MORE

answered Apr 7 in PHP by Niroj
• 58,500 points
630 views
0 votes
1 answer

How can I get ID of the last updated row in MySQL?

Hello @kartik, Suppose, item_id is an integer identity column in items table ...READ MORE

answered Apr 9 in PHP by Niroj
• 58,500 points
520 views
0 votes
1 answer

How to implement a callback in PHP?

Hello, Implementation of a callback is done like ...READ MORE

answered Apr 15 in PHP by Niroj
• 58,500 points
49 views
0 votes
0 answers

Anyone can help me out to understand the semantic of (document.getElementBYId("demo").innerHTML="Hello") ?

Hello guys, Can Someone helps me to find ...READ MORE

Jan 17 in Web Development by anonymous
• 27,140 points
85 views
+1 vote
1 answer

What is the relationship between angularjs Scope with controller/view?

Let us consider the below block: <div ng-controller="emp"> ...READ MORE

answered Jan 20 in Web Development by Niroj
• 58,500 points

edited Jan 21 by Niroj 125 views
0 votes
1 answer

What is data binding in AngularJS?

Data binding is synchronization of data between the ...READ MORE

answered Jan 23 in Web Development by Niroj
• 58,500 points
68 views
0 votes
1 answer

How to resolve the problem of losing a session after a redirect in PHP?

Hello @kartik, Carry out these usual checks: Make sure session_start(); is ...READ MORE

answered Aug 24 in PHP by Niroj
• 58,500 points
194 views
0 votes
1 answer

How can we create a session in PHP?

Hello @kartik, session_start() function is used in PHP to ...READ MORE

answered Mar 27 in PHP by Niroj
• 58,500 points
71 views