2016-08-19 4 views
-3

正しい場所にカンマを追加するためのjqueryコードがあります。もう一度jqueryを使用したくありません。どのようにカンマを追加するためにphpを使用するのですか? 怒鳴るは私の試みphpを使用してコンマを追加

PHPは

<?php 

function numberWithCommas(x) { 
    return x.str_replace(/\B(?=(?:\d{3})+(?!\d))/g, ","); 
} 
$bal = numberWithCommas($UserCradit); 
echo $ubal; 
?> 
+1

探しているものです。http://php.net/manual/en/ class.numberformatter.php ...また、あなたの構文はオフです 'x.str_replace'は* PHP *ではありません。 – CD001

答えて

1

これは間違っています:x.str_replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");オーバーフローする前にPhp Manualを使用することを学ぶ必要があります。 number_formathttp://php.net/manual/en/function.number-format.php)は、そのためのクラスがあります...あなたはPHPの書式国際化通貨のいくつかの種類をしようとしている場合は、

<?php 
    $bal = number_format($UserCradit); 
    echo $bal; 
?> 
-1

変更return x.str_replace(/\B(?=(?:\d{3})+(?!\d))/g, ","); return str_replace(/\B(?=(?:\d{3})+(?!\d))/g, "," ,$x)

しようとした

<script type="text/javascript"> 
//<![CDATA[ 
$(window).load(function(){ 
function numberWithCommas(x) { 
    return x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ","); 
} 


    $("#numbal").each(function(){ 
    var bal_cc = $(this).html(); 
    bal_cc = numberWithCommas(bal_cc); 

    $(this).html(bal_cc) 

     }) 


});//]]> 

</script> 

jqueryのですはPHPの機能ではありません。 php manual str_replace

+0

RegExの周りの一重引用符を叩いてpreg_replace;を使用したい場合があります) – CD001

2

あなたは正規表現を使用して交換する場合は、このコードでなければなりません

preg_replace() 

を使用する必要があります:もっとここに行くために

<?php 

function numberWithCommas($x) { 
    return preg_replace('/\B(?=(?:\d{3})+(?!\d))/g', ",", $x); 
} 

$bal = numberWithCommas($UserCradit); 
echo $ubal; 
?> 

x.str_replaceは()どのようにではありませんPHP関数にアクセスする。また、str_replace()は正規表現には使用されません。

関連する問題