0

when i remove money characters then it work but i must require this chars

echo "Product Name :: ".$product_name = '£5 off bookings at A1Travel';

echo "<br/>".$product_name = preg_replace('/(^([^a-zA-Z0-9$¢£€¥])*|([^a-zA-Z0-9$¢£€¥])*$)/', '', $product_name);

Output is :

‚£5 off bookings at A1Travel

Desired output:

£5 off bookings at A1Travel
Yatin Mistry
  • 1,246
  • 2
  • 13
  • 35

4 Answers4

2

You have added ^ and $ which means it would only match at the beginning or end of string. So you need to remove them:

/(([^a-zA-Z0-9$¢£€¥])*|([^a-zA-Z0-9$¢£€¥])*)/

This means your regex could be simplified to:

/[^a-zA-Z0-9$¢£€¥]*/

Now let's improve it further. You never want to replace an empty string with nothing. So let's change the quantifier from * (zero or more times) to + (one or more times):

/[^a-zA-Z0-9$¢£€¥]+/

You'll notice that this also removes spaces, so let's exclude it as well:

/[^a-zA-Z0-9$¢£€¥ ]+/

If you want to make it shorter, let's use the i modifier to match case insensitive and remove A-Z:

/[^a-z0-9$¢£€¥ ]+/i

After some debugging, it seems we need to handle utf/unicode problems. So let's add the u modifier like nhahtdh said:

/[^a-z0-9$¢£€¥ ]+/ui

To test this, you will also need to set the encoding for your HTML page. A simple example would look like this:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <?php
            $product_name = '£5 off bookings at A1Travel';
            echo 'Product Name :: ' . $product_name . '<br>';

            $product_name = preg_replace('/[^a-z0-9$¢£€¥ ]+/ui', '', $product_name);

            echo $product_name . '<br>';
        ?>
    </body>
</html>

regex demo

Community
  • 1
  • 1
HamZa
  • 14,671
  • 11
  • 54
  • 75
  • I have run at https://regex101.com/r/iQ5oT9/1 here it works but in my php page it is not working – Yatin Mistry Dec 22 '14 at 10:30
  • 1
    All PHP regex should use `u` flag regardless, since non-UTF mode doesn't work on Unicode. Note that regex101 compiles from 16-bit library, while PHP uses 8-bit library. – nhahtdh Dec 22 '14 at 10:44
  • @YatinMistry See edit. You will need to use the `u` modifier like @nhahtdh said. Also you need to set the encoding of your html page to utf-8. – HamZa Dec 22 '14 at 10:52
  • @Hamza and nhahtdh thanks a lot it works now. Live server i have tried it without meta tag and it works. – Yatin Mistry Dec 22 '14 at 11:04
  • @YatinMistry I would check [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) out. Maybe your live server is sending the page encoding along the headers... – HamZa Dec 22 '14 at 11:08
0

You can use:

$input = '£5 off bookings at A1Travel';
$input = preg_replace('/[^\x00-\x7f£¢£€¥]+/u', '', $input);
//=> £5 off bookings at A1Travel

RegEx Demo

anubhava
  • 761,203
  • 64
  • 569
  • 643
0

please try this

$str= '£5 off bookings at A1Travel';
preg_replace('/[^\p{Latin}\d ]/u', '', $str);
Arun
  • 750
  • 5
  • 12
0

you can try this method also it will be much helpful

To remove â character from string

mysqli_set_charset($con,"utf8");

$price = "₹ 310.00";

$price2 = preg_replace('/[^(\x20-\x7F)]*/','', $price);

Result : 310.00

this helps to remove character like Remove  from string using preg_replace

Thanks

JIJOMON K.A
  • 1,290
  • 3
  • 12
  • 29
Solomon Suraj
  • 1,162
  • 8
  • 8