Kristoff1875 Posted January 17, 2012 Share Posted January 17, 2012 Hi Guys, I've got the following on a WP Theme that uses fields for prices: function clean_price($string) { $result = preg_replace("/[^0-9.,]/","", $string); return $result; } Which is then called here: if (!empty($_POST['price'])) $_POST['price'] = clean_price($_POST['price']); But for some reason you can put in £100GBP and that is exactly what is being returned in the page. Anyone got any ideas why? Thanks in advanced Quote Link to comment https://forums.phpfreaks.com/topic/255205-problem-with-preg-replace/ Share on other sites More sharing options...
litebearer Posted January 17, 2012 Share Posted January 17, 2012 I tested your code, It works. So make sure that the portion where the value is being echoed is NOT formatting the output. Quote Link to comment https://forums.phpfreaks.com/topic/255205-problem-with-preg-replace/#findComment-1308501 Share on other sites More sharing options...
laffin Posted January 17, 2012 Share Posted January 17, 2012 Because your matching a substring use ^ & $ to denote the begin/end of a string /[^0-9.,]/ I really don't understand your pattern for the sample you gave, as this pattern will match any character not being 0-9 . , Quote Link to comment https://forums.phpfreaks.com/topic/255205-problem-with-preg-replace/#findComment-1308512 Share on other sites More sharing options...
Kristoff1875 Posted January 17, 2012 Author Share Posted January 17, 2012 @litebearer: Thanks, not sure why it's not working on the site if you've got it to work? When it's called to the site, this is the code: <p class="bigprice"><?php get_price($post->ID); ?></p> In the theme functions.php file that the clean_price function is in, there is this: // get the ad price and position the currency symbol function get_price($postid) { if(get_post_meta($postid, 'price', true)) { $price_out = get_post_meta($postid, 'price', true); // uncomment the line below to change price format //$price_out = number_format($price_out, 2, '.', ','); $price_out = pos_currency($price_out); } else { $price_out = ' '; } echo $price_out; } // pass in the price and get the position of the currency symbol function pos_price($numout) { $numout = pos_currency($numout); echo $numout; } // figure out the position of the currency symbol and return it with the price function pos_currency($price_out) { if (get_option('curr_symbol_pos') == 'left') $price_out = get_option('curr_symbol') . $price_out; elseif (get_option('curr_symbol_pos') == 'left_space') $price_out = get_option('curr_symbol') . ' ' . $price_out; elseif (get_option('curr_symbol_pos') == 'right') $price_out = $price_out . get_option('curr_symbol'); else $price_out = $price_out . ' ' . get_option('curr_symbol'); return $price_out; } But I can't really make heads nor tales of that, as far as I can see there isn't anything that should affect the removing of the values that shouldn't be there? @Laffin: So should it be? $result = preg_replace("/[^0-9.,&]/","", $string); I basically only want numbers and ,. to be bosted to the page. So if someone posts the value: £100 it removes the £ sign as this gets added on. At the moment you can type "£100GBP" and this shows up as: "££100GBP" as there is a default £ on the page. Quote Link to comment https://forums.phpfreaks.com/topic/255205-problem-with-preg-replace/#findComment-1308533 Share on other sites More sharing options...
litebearer Posted January 17, 2012 Share Posted January 17, 2012 Here is your original code with a couple of example strings. <?PHP $price = "£100,000GBP"; function clean_price($string) { $result = preg_replace("/[^0-9.,]/","", $string); return $result; } echo "Before: " . $price . "<br />"; echo "After: "; echo clean_price($price); $price = "A'<BR>'123,x.00 , . yes now <img> 3986.789"; echo "<br/>"; echo "Before: " . $price . "<br />"; echo "After: "; echo clean_price($price); ?> working sample http://www.nstoia.com/price.php Quote Link to comment https://forums.phpfreaks.com/topic/255205-problem-with-preg-replace/#findComment-1308541 Share on other sites More sharing options...
Kristoff1875 Posted January 17, 2012 Author Share Posted January 17, 2012 Thanks for that litebearer. Not quite sure what to do now. It clearly should be working, but for some reason somewhere in the theme it's being messed up. Could it be anything to do with: if (!empty($_POST['price'])) $_POST['price'] = clean_price($_POST['price']); ? Quote Link to comment https://forums.phpfreaks.com/topic/255205-problem-with-preg-replace/#findComment-1308542 Share on other sites More sharing options...
litebearer Posted January 17, 2012 Share Posted January 17, 2012 Try changing this $_POST['price'] = clean_price($_POST['price']); to this $price = clean_price($_POST['price']); HOWEVER, I suspect that within the code it is formatting the value with the appropriate currency symbols Quote Link to comment https://forums.phpfreaks.com/topic/255205-problem-with-preg-replace/#findComment-1308543 Share on other sites More sharing options...
litebearer Posted January 17, 2012 Share Posted January 17, 2012 BTW I personally NEVER alter the values of $_POST to themselves ie $_POST['a1'] = change_value_of($_POST['a1']). It makes it easier to test my values when need to find errors/problems Quote Link to comment https://forums.phpfreaks.com/topic/255205-problem-with-preg-replace/#findComment-1308545 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.