Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ereg_replace, need to replace the $ with nothing. 1

Status
Not open for further replies.

jewel464g

Technical User
Jul 18, 2001
198
US
ok, I want to replace the $ in this string with nothing. So I wrote this little test script and it doesn't work. I'm sure it has something to do with the $ because when I tell it to replace the 2 with nothing it works. Any ideas?

<?
$price ="\$1234";

$repl ="";
$hjk = (ereg_replace("\$", $repl, $price));
echo $hjk;
?>

When faced with a decision, always ask, 'Which would be the most fun?'
 
Why don't you just use the str_replace() function

Code:
<?
$str = '$1234';
echo str_replace('$','',$str);
?>

Also, why the backslash before the "$"? Just enclose your string in single quotes and don't use the backslash.

Ken
 
Some explanation:

The dollar sign in regular expressions has some specific meaning. Here's some comments/advice:

1. Don't use ereg. Use preg.
2. The dollar sign as is refers to the end of a subject. It needs to be double escaped to work as a literal dollar sign.
3. Be aware of the difference between double and single quotes. '$1234' works fine, no escaping necessary. Use single quotes for literals - it will make sure no variable substitution will happen.
4. Using preg this expression works:
Code:
$price ='$1234';
$repl ="";
$hjk = (preg_replace("/\\$/", $repl, $price));
echo($hjk);

However, a regular expression for such a simple task is too much overhead. The str_replace is the better solution.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top