Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

formatting with a string using php

Tags:

php

mysql

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

like image 209
user3305327 Avatar asked Sep 22 '14 10:09

user3305327


People also ask

What does %s mean in PHP?

%s is a type specifier which will be replaced to valuable's value (string) in case of %s . Besides %s you can use other specifiers, most popular are below: d - the argument is treated as an integer, and presented as a (signed) decimal number.

What is a string formatting?

String formatting is also known as String interpolation. It is the process of inserting a custom string or variable in predefined text. custom_string = "String formatting" print(f"{custom_string} is a powerful technique") Powered by Datacamp Workspace. String formatting is a powerful technique.


1 Answers

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.

like image 85
Pred Avatar answered Oct 23 '22 23:10

Pred