简体   繁体   English

在 javascripts 和 php 中连接数字

[英]Concatenating numbers in javascripts and php

I translate some code from javascript to php, my problem is with numbers, because in javascript "+" is used in both situation, addition and concatenating.我将一些代码从 javascript 翻译成 php,我的问题是数字,因为在 javascript 中,“+”用于加法和连接两种情况。 ex:前任:

1+1+ "2" +"4"+3 - 5 -1+"" + 2 

in javascript result is 22372在 javascript 结果是 22372

How can I make this in php?我怎样才能在 php 中做到这一点? I try with preg_replace我尝试使用 preg_replace

1+1 --> 2
2+"2" --> "22" // as string
""+2 --> "2" 

problem is here:问题在这里:

(1+1+ "2" +"4"+3 - 5 -1)+""+2

I need to solve () then concatenating with "" and 2我需要解决 () 然后连接 "" 和 2

String concatenation is done with "."字符串连接是用“.”完成的。 rather than "+" in php.而不是 php 中的“+”。 You can still concatenate values of different types, eg您仍然可以连接不同类型的值,例如

$apple_count = 25;
$my_string = "I want " . $apple_count . " apples.";

I work on a solution我正在研究解决方案

echo $a." = 22372"."\n";
function concat_num($a) {
 while (preg_match("/\"(\d+)\"\s*\+\s*(\d+)/",$a) || preg_match("/^(?!\-)\s*(\d+)\s*\+\s*\"(\d+)\"/",$a)) { // "10" + 2 or 2 + "10"
 while (preg_match("/(\d+)\s*\+\s*(\d+)/",$a)) {    //1 + 2 --> 3
  $a=preg_replace_callback(
    "/(\d+)\s*\+\s*(\d+)/",
    function ($m) {
      return floor($m[1]+$m[2]);
    },
    $a
    );
 }
 echo $a."\n";
 while (preg_match("/\"\"\s*\+\s*(\d+)/",$a)) {
  $a=preg_replace("/\"\"\s*\+\s*(\d+)/",'"'."\$1".'"',$a);  // ""+11   ---> "11"
 }
 echo $a."\n";
 while (preg_match("/(\d+)\s*\+\s*\"\"/",$a)) {
  $a=preg_replace("/(\d+)\s*\+\s*\"\"/",'"'."\$1".'"',$a);  // 11+""   ---> "11"
 }
 echo $a."\n";
 while (preg_match("/\"(\d+)\"\s*\+\s*\"\"/",$a)) {
  $a=preg_replace("/\"(\d+)\"\s*\+\s*\"\"/",'"'."\$1".'"',$a); // "11"+"" --> "11"
 }
 echo $a."\n";
 while (preg_match("/\"\"\s*\+\s*\"(\d+)\"/",$a)) {
  $a=preg_replace("/\"\"\s*\+\s*\"(\d+)\"/",'"'."\$1".'"',$a);  // ""+"22" -->"22"
 }
 echo $a."\n";
 while (preg_match("/\"(\d+)\"\s*\+\s*\"(\d+)\"/",$a)) {
  $a=preg_replace("/\"(\d+)\"\s*\+\s*\"(\d+)\"/",'"'."\$1\$2".'"',$a); // "11"+"22" --> "1122"
 }
 echo $a."\n";
 while (preg_match("/\"(\d+)\"\s*\+\s*(\d+)/",$a)) {
  $a=preg_replace("/\"(\d+)\"\s*\+\s*(\d+)/",'"'."\$1\$2".'"',$a); // "11"+22 -->> "1122"
 }
 echo $a."\n";
 while (preg_match("/^(?!\-)\s*(\d+)\s*\+\s*\"(\d+)\"/",$a,$m)) {
 $a=preg_replace("/^(?!\-)\s*(\d+)\s*\+\s*\"(\d+)\"/",'"'."\$1\$2".'"',$a); // 11+"22" --->> "1122"
 }
 echo $a."\n";
 while (preg_match("/\"(\d+)\"\s*\-\s*(\d+)/",$a)) {  // "10" - 8 --> 2
  $a=preg_replace_callback(
    "/\"(\d+)\"\s*\-\s*(\d+)/",
    function ($m) {
      return floor($m[1]-$m[2]);
    },
    $a
  );
 }
 echo $a."\n";
 while (preg_match("/(\d+)\s*\-\s*(\d+)/",$a)) {    //5 - 2 --> 3
  $a=preg_replace_callback(
    "/(\d+)\s*\-\s*(\d+)/",
    function ($m) {
      return floor($m[1]-$m[2]);
    },
    $a
    );
 }
 echo $a."\n";
 }
 return $a;
}
echo $b=concat_num($a)."\n";
$code="\$d=".$b.";";
eval ($code);
echo $d."\n";

And result结果

1+1+ "2" +"4"+3 - 5 -1+"" + 2 = 22372
2+ "2" +"4"+3 - 5 -1+"" + 2
2+ "2" +"4"+3 - 5 -1+"2"
2+ "2" +"4"+3 - 5 -1+"2"
2+ "2" +"4"+3 - 5 -1+"2"
2+ "2" +"4"+3 - 5 -1+"2"
2+ "24"+3 - 5 -1+"2"
2+ "243" - 5 -1+"2"
"2243" - 5 -1+"2"
2238 -1+"2"
2237+"2"
2237+"2"
2237+"2"
2237+"2"
2237+"2"
2237+"2"
2237+"2"
2237+"2"
"22372"
"22372"
"22372"
"22372"
22372

This seems to work but the best solution is to evaluate string from beginning to end.这似乎有效,但最好的解决方案是从头到尾评估字符串。

1+1 --> 2 (num)
2+"2" --> "22" (string)
"22"+"4" --> "224" (string)
"224"+3 --> "2243" (string)
2243-5 --> 2238 (num)
2238-1 --> 2237 (num)
2237+"" --> "2237" (string)
"2237"+"2" --> "22372" (string)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM