Jump to content

Problem with preg replace?


Kristoff1875

Recommended Posts

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

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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']);

 

?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.