如何将 php 中的数学表达式转换为字符串?
How to convert a math expression into string in php?
我只是想找到一种方法将数学表达式转换为字符串。有很多解决方案可以将字符串转换为数学形式或计算字符串中的数学表达式,但没有一个解决我的问题。
$math_expression = 10/24/2020;
$result = strval($math_expression);
echo $result;
输出为:
0.00020627062706271
我也试过了
(string)$math_expression
但是输出是一样的0.00020627062706271
。
我希望输出为 '10/24/2020'
。
控制器函数如下:
public function importScores(Request $request)
{
$fileName = time() . '.' . $request->file->getClientOriginalExtension();
$request->file->move(storage_path('upload'), $fileName);
$filePath = storage_path('upload/' . $fileName);
Excel::import(new ScoresImport, $filePath);
$scores = Score::all();
return response()->json(['scores' => $scores, 'request' => $filePath], 200);
}
这是 ScoresImport Class:
<?php
namespace App\Imports;
use App\Models\Score;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
class ScoresImport implements ToCollection
{
public function collection(Collection $rows)
{
foreach ($rows as $key => $row) {
if ($key > 0) {
Score::create([
'user_id' => $row[0],
'result' => $row[1],
'grade_date' => Carbon::createFromFormat('m/d/Y', $row[2])->format('m/d/Y'), //Coming from excel file in 11/23/2020 MM/dd/yyy format
// 'grade_date' => $row[2], // I also tried as this and with (string)$row[2] && strval($row[2])
'ie' => $row[3]
]);
}
}
}
public function chunkSize(): int
{
return 1000;
}
public function batchSize(): int
{
return 1000;
}
}
我认为这是不可能的,只要你不将数据作为字符串导入,因为变量保存了你导入的值,它已经计算了值。
也许您应该使用 Excel 中的 CONCATENATE 函数在表达式前后添加引号。
嗯,我认为这与 PHP 无关,因为在任何情况下都会计算所有数学运算。
我没用过你说的包。但是通过查看他们的代码,我意识到它正在使用另一个名为“PhpOffice\PhpSpreadsheet”的包作为 dependency. The package has a function named excelToDateTimeObject,我认为这将帮助您解决挑战。
因此,如果我假设您正在使用 Laravel-Excel 的 version:3.1,您的控制器将类似于:
<?php
// ...
use PhpOffice\PhpSpreadsheet\Shared\Date;
class ScoresImport implements ToCollection
{
public function collection(Collection $rows)
{
foreach ($rows as $key => $row) {
if ($key > 0) {
$phpDateObject = Date::excelToDateTimeObject($row[2]);
$gradeDateToCreate = Carbon::instance($phpDateObject)->format('m/d/Y');
Score::create([
'user_id' => $row[0],
'result' => $row[1],
'grade_date' => $gradeDateToCreate,
'ie' => $row[3]
]);
}
}
}
// ...
}
我只是想找到一种方法将数学表达式转换为字符串。有很多解决方案可以将字符串转换为数学形式或计算字符串中的数学表达式,但没有一个解决我的问题。
$math_expression = 10/24/2020;
$result = strval($math_expression);
echo $result;
输出为:
0.00020627062706271
我也试过了
(string)$math_expression
但是输出是一样的0.00020627062706271
。
我希望输出为 '10/24/2020'
。
控制器函数如下:
public function importScores(Request $request)
{
$fileName = time() . '.' . $request->file->getClientOriginalExtension();
$request->file->move(storage_path('upload'), $fileName);
$filePath = storage_path('upload/' . $fileName);
Excel::import(new ScoresImport, $filePath);
$scores = Score::all();
return response()->json(['scores' => $scores, 'request' => $filePath], 200);
}
这是 ScoresImport Class:
<?php
namespace App\Imports;
use App\Models\Score;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
class ScoresImport implements ToCollection
{
public function collection(Collection $rows)
{
foreach ($rows as $key => $row) {
if ($key > 0) {
Score::create([
'user_id' => $row[0],
'result' => $row[1],
'grade_date' => Carbon::createFromFormat('m/d/Y', $row[2])->format('m/d/Y'), //Coming from excel file in 11/23/2020 MM/dd/yyy format
// 'grade_date' => $row[2], // I also tried as this and with (string)$row[2] && strval($row[2])
'ie' => $row[3]
]);
}
}
}
public function chunkSize(): int
{
return 1000;
}
public function batchSize(): int
{
return 1000;
}
}
我认为这是不可能的,只要你不将数据作为字符串导入,因为变量保存了你导入的值,它已经计算了值。
也许您应该使用 Excel 中的 CONCATENATE 函数在表达式前后添加引号。
嗯,我认为这与 PHP 无关,因为在任何情况下都会计算所有数学运算。
我没用过你说的包。但是通过查看他们的代码,我意识到它正在使用另一个名为“PhpOffice\PhpSpreadsheet”的包作为 dependency. The package has a function named excelToDateTimeObject,我认为这将帮助您解决挑战。
因此,如果我假设您正在使用 Laravel-Excel 的 version:3.1,您的控制器将类似于:
<?php
// ...
use PhpOffice\PhpSpreadsheet\Shared\Date;
class ScoresImport implements ToCollection
{
public function collection(Collection $rows)
{
foreach ($rows as $key => $row) {
if ($key > 0) {
$phpDateObject = Date::excelToDateTimeObject($row[2]);
$gradeDateToCreate = Carbon::instance($phpDateObject)->format('m/d/Y');
Score::create([
'user_id' => $row[0],
'result' => $row[1],
'grade_date' => $gradeDateToCreate,
'ie' => $row[3]
]);
}
}
}
// ...
}