Formatting with string using php

I have a line like this 2 661,38 € and I need an echo of 661,38 , but I can't get to the solution

I made the following code:

 $connection=mysql_connect('localhost','root','****') or die(mysql_error()); mysql_select_db('DB',$connection) or die(mysql_error()); $sql = mysql_query("select distinct prod_price COL from TABLE") or die(mysql_error()); ?> <?php while($row=mysql_fetch_object($sql)):?> <?php if(mysql_num_rows($sql)>0){ $number = $row->COL; $temp=explode(' ',$number); $number = preg_replace("/[^0-9]/", '', $temp[0])/100; echo number_format($number, 2, ',', ' '). "<br />"; } ?> <?php endwhile;?> 

Can anyone help me remove this 2 from first place?

The solution I got does not help me, so I put all the code in order to understand the situation I'm stuck in ... see the code snippet ... The above code gives me the following notification:

Note: Undefined offset: 2 in /var/www/html/login/str.php on line 26 0.00

+7
php mysql
source share
4 answers

If you convert the string 2 661,38 € ASCII to hexadecimal numbers, you will see the following sequence: 32 C2 20 36 36 31 2C 33 38 C2 20 E2 82 AC

This sequence in UTF-8 has 2 661,38 € with two escape characters ( \xC2 ).

I think this is strictly a coding problem, so you need to convert the source strings (with the original character set) to UTF-8.

You can do this in MySQL with the following expression:

 CONVERT(CONVERT(YourColumn USING binary) USING utf8) 

This probably returns NULL if the original value cannot be converted to UTF8, but if it is a valid UTF-8 sequence, it will be converted.

Also, please make sure that the connection and encoding of PHP files is also UTF-8. Different encodings can lead to unexpected behavior.

+1
source share

You split this line with a space, ten apply preg_replace ()

 <?php $number = "2 661,38 €"; $temp=explode(' ',$number); $number = preg_replace("/[^0-9]/", '', $temp[1])/100; echo number_format($number, 2, ',', ' '); ?> 

Demo

0
source share

First you need to modify the DB table. Go to the table structure and change the mapping setting to utf8_general_ci, then you need to set the header

0
source share

This will get rid of everything except numbers and commas:

 $number = preg_replace("/[^0-9,]*/", '', $your_weird_pattern); 
0
source share

All Articles