?
Solved

simple ereg_replace question

Posted on 2006-05-31
5
Medium Priority
?
162 Views
Last Modified: 2006-11-18
i have a string which contains a $ in it... ie $3.50 and I want to replace the $ with a $$ or 'Dollars'

Help?
0
Comment
Question by:phenixfilms
  • 2
  • 2
5 Comments
 
LVL 21

Accepted Solution

by:
Julian Matz earned 150 total points
ID: 16804129

Try this:

$string = '$3.50';

str_replace('$','$$',$string);
str_replace('$','Dollars',$string);
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 16804819
Above works. However if you want to only replace dollars in front of numbers, use this:

$string = preg_replace('/\$\s*(\d+(\.\d+)?)/', '$$\1', $string);
or
$string = preg_replace('/\$\s*(\d+(\.\d+)?)/', '\1 Dollars', $string);

-r-
0
 
LVL 11

Expert Comment

by:neester
ID: 16823915
Simple:

str_replace('$','$$',$yourString);

or

str_replace('$','Dollars',$yourString);

where $yourString is the string you have.
0
 
LVL 21

Expert Comment

by:Julian Matz
ID: 16823976

neester, that is more-or-less the exact same as the answer that I've given in my first post...
0
 
LVL 11

Expert Comment

by:neester
ID: 16825573
Yeah - I just dont know why you used the ascii codes - it could of confused the asker.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month17 days, 2 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question