7

I have a string like this 2 661,38 € and I need to echo 661,38 but I can't reach to the solution

I have done 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 please help me to remove that 2 from the first place ?

The solution I got is not helping me so I am putting the whole code to understand the situation I am got stuck...please see the code snippet...The above code is giving me the following Notice:

Notice: Undefined offset: 2 in /var/www/html/login/str.php on line 26 0,00

7
  • 12
    Where does the string come from? It looks like a misinterpreted UTF-8 string - maybe you should fix encoding issues first. Commented Sep 22, 2014 at 10:31
  • its coming from the DB Commented Sep 22, 2014 at 10:31
  • You will be putting more load on application layer, even when this can be handled efficiently in the database layer! You have to change the database character set to utf-8 and collation to utf8_general_ci and then unicode are supported by your database, sparing you the headache of what you are intending to do now. Commented Sep 22, 2014 at 11:10
  • @freerunner I have changed it but have no effect in the result Commented Sep 22, 2014 at 11:38
  • 1
    Based on my researches, the first digit (2) is required too. I think, the original string was 2 661,38 €. Try to convert the original strings to UTF-8 with the following expression: CONVERT(CONVERT(YourColumn USING binary) USING utf8) (this probably returns NULL if the original value could not be converted to UTF8, but if it is a valud UTF-8 sequence, then it will will be converted). Also plese be sure that the connection's and the PHP file's encoding is UTF-8 too. Commented Sep 22, 2014 at 13:30

4 Answers 4

1

If you convert the 2 661,38 € ASCII string to hexadecimal numbers, then 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 is 2 661,38 € with two control characters (\xC2).

I think, this is strictly an encoding problem, so you have to convert the original 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 could not be converted to UTF8, but if it is a valid UTF-8 sequence, then it will will be converted.

Also please be sure that the connection's and the PHP file's encoding is UTF-8 too. Different encodings could result to unexpected behavior.

Sign up to request clarification or add additional context in comments.

Comments

0

You split that string with 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

4 Comments

Please explain how this code helps to solve the original problem. The goal of the answers are not only to solve the original problem, but to help further readers of this thread understand both the base problem and the solution. Code-only ansers are considered as low quality posts.
@Pred Is it not a solution?
It could be a solution, but if someone reads this post in the future with limited or zero knowledge related to regexp or php, the code itself probably will not help. A short explanation why and how this answer (how the provided regexp and code) help or solve the original problem could be really helpful.
OP should decide and as @Pred gave an insight that this is not 661,38 but 2 661,38 € , @user3305327 did not specify if this is the only string he is having a trouble with. And I know he has some more, and this solution will not work or may be risky to use
0

First you need to change in you DB table. Go to your table structure and change collation setting to utf8_general_ci, then you need to set header

Comments

0

This will get rid of everything except numbers and commas:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.