Shahadat Atom
Shahadat Atom

Reputation: 334

How to replace "$" sign from any string with something else in PHP?

I am working with a wordpress project. where using some money related work. where amount in database.the amount shows with a $ sign. i want to replace this $ sign with EURO sign. but when i using str_replace function it took the $amount as a variable, not as a string. what is the way to replace this $ ?

Here echo product_list_price($project->ID); function returning the price containing $ sign. now i have to replace this sign into EURO sign and then to echo. how can i convert this as a single quoted string?

Thanks in advance

Upvotes: 0

Views: 162

Answers (2)

BVengerov
BVengerov

Reputation: 3007

$euroPrice = str_replace('$', '€', $dollarPrice);

As mentioned here, PHP tries to interpret words starting with $ inside double-quoted strings as variables.

Upvotes: 3

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

The works fine, str_replcae will give you new modified string that you need to store, the old one will not be modified

    <?php
$money="44$";
$money2='44$';
$m1 = str_replace("$","€",$money);
$m2 =str_replace("$","€",$money2);
$m3 = str_replace('$','€',$money2);
echo $money." ".$money2." ";
echo $m1." ".$m2." ".$m3;
?>

output:

44$ 44$ 44€ 44€ 44€

Upvotes: 1

Related Questions