• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Trying to remove commas from text form entry

 
Greenhorn
Posts: 6
PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I am trying to get my PHP form, which is linked to an HTML form, to remove all commas from user-entered figures. This is because I want to get the subtotal of several dollar amounts entered, but the commas seem to be read as decimals in the subtotal.

So I looked around and put together a solution that, I'm surprised, does not work:



When I run my program, I get the following error:

Warning: Use of undefined constant “” - assumed '“”' (this will throw an Error in a future version of PHP)

Then it goes on to calculate the subtotal as if the commas were periods.

It might be important to note that I have (cast?) all my totals to be subtotaled as floats using floatval, and I have rounded $subtotal to two decimal places using number_format.

Any insights into this would be appreciated.
 
Saloon Keeper
Posts: 2940
373
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have a couple of problems.

1. The quotes that you show for the replacement string look odd.  I've seen this when a word processor is used to edit the file rather than a text editor.

2. You aren't doing anything with the value returned by str_replace.

Also, I wouldn't bother checking to see if there was any single quotes in $item, just process it withstr_replace each time.  If there are no single quotes in $item, the value returned by str_replace will be the same.

 
Saloon Keeper
Posts: 21595
146
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:
1. The quotes that you show for the replacement string look odd.  I've seen this when a word processor is used to edit the file rather than a text editor.



In fact, in our message editor, you'll notice that they display in a completely different color than the regular quote characters do. Plus, of course, the error message itself: " Use of undefined constant “” "

There are 3 kinds of double-quote marks in general use (we'll ignore guillemots, since they're more specialized). The left double quote, the right double quote and the symmetrical double quote, which is the one we usually mean in computerspeak when we say "quote". Left and right double quotes are used in English and some other language typesetting and the open sides of the quotes encapsulate the quoted text.  Very few programming languages use the asymmetrical quote characters as syntactic elements. Almost always you'd use the "real" (symmetric) quote character for that.

That's not even counting the single-quote versions, since properly a symmetrical single quote, which is an apostrophe. Or the backtick, of Unix fame (`).

And, as Ron pointed out, even had you not had that problem, you were discarding the results without using them. The str_replace doesn't literally replace, it creates a new version of the string with the offending characters replaced within it.

Don't use word-processing programs (including Windows Wordpad) for program editing. Not only are they likely to automatically substitute the literary versions of the quote and apostrophe characters (as well as possibly dashes and other forms), but there's a chance that test format-related garbage will also be saved with the text. and PHP won't like that at all.
 
Brenda Anne
Greenhorn
Posts: 6
PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! Problem solved.
 
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!