Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从字符串中获取数字,然后进行计算_Php - Fatal编程技术网

Php 从字符串中获取数字,然后进行计算

Php 从字符串中获取数字,然后进行计算,php,Php,我有一根这样的绳子 $coordinate = "coords='429, 457, 421, 460, 424, 464, 433, 465, 433, 460'"; 我想将数字乘以2,如何编写一个简单的php代码来进行计算 这个新的$坐标应该是 coords="858, 914, 842, 920, 848, 928, 866, 930, 866, 920" 我的原始字符串是“alt='Japan'shape='poly'coords='4294574211460424464334654

我有一根这样的绳子

$coordinate = "coords='429, 457, 421, 460, 424, 464, 433, 465, 433, 460'";
我想将数字乘以2,如何编写一个简单的php代码来进行计算

这个新的$坐标应该是

coords="858, 914, 842, 920, 848, 928, 866, 930, 866, 920"
我的原始字符串是“alt='Japan'shape='poly'coords='429457421146042446433465433460'”

类似于:

$coords="429, 457, 421, 460, 424, 464, 433, 465, 433, 460";
$coords_arr = explode(",", $coords);
array_walk($coords_arr, 'alter');

function alter(&$val) {
    $val *= 2; //multiply by 2
}
print_r($coords_arr);
更新代码::

$coordinate = "coords='429, 457, 421, 460, 424, 464, 433, 465, 433, 460'";
$arr = explode("=", $coordinate);
$data = trim($arr[1], "'"); //remove quotes from start and end
$coords=explode(",", $data);

array_walk($coords, 'alter');

function alter(&$val) {
    $val = (int) $val * 2;
}
echo "<pre>";
print_r($coords);
$coordinate=“coords='429457421146434433465433460';
$arr=分解(“=”,$coordinate);
$data=trim($arr[1],“”)//从开始和结束删除引号
$coords=explode(“,”,$data);
数组_walk($coords,'alter');
函数alter(&$val){
$val=(int)$val*2;
}
回声“;
打印(coords);

假设原始数组定义为

$newCoords = implode(", ",array_map(function($a) { return $a *2; }, explode(",", $coords)));
你可以用,和。请注意,此处使用的匿名函数仅适用于PHP5.3及以上版本

$coordinate = 'coords="429, 457, 421, 460, 424, 464, 433, 465, 433, 460"';

$start = strpos($coordinate,'"');
$end = strrpos($coordinate,'"');

$str = substr($coordinate,$start + 1, ($end - $start -1));

$val_a = explode(', ',$str);

$new_str = '';
foreach ($val_a as $val_1) {
    $val_i = (int)$val_1 * 2;
    if ($new_str) $new_str .= ", $val_i";
    else $new_str = "$val_i";
}

echo 'coords="'.$new_str.'"';

上面示例中的工作代码。。。注意引号中“to”的变化


您可以先去掉所有不需要的文本,然后像这样调用array_map:


分解成数组,然后循环,我认为这甚至不能正确解释。字符串不是如您所写的那样以等号结尾吗?结果应该是这样的,$coordinate=“coords=”858、914、842、920、848、928、866、930、866、920”“;“coords=”是语法无效的字符串:
$coordinate=“coords=”="429, 457, 421, 460, 424, 464, 433, 465, 433, 460"";您正在跳过一些步骤,如何获得$coords=“4294574211460424464433465433460”@alexalex添加了完整的代码
$coordinate = 'coords="429, 457, 421, 460, 424, 464, 433, 465, 433, 460"';

$start = strpos($coordinate,'"');
$end = strrpos($coordinate,'"');

$str = substr($coordinate,$start + 1, ($end - $start -1));

$val_a = explode(', ',$str);

$new_str = '';
foreach ($val_a as $val_1) {
    $val_i = (int)$val_1 * 2;
    if ($new_str) $new_str .= ", $val_i";
    else $new_str = "$val_i";
}

echo 'coords="'.$new_str.'"';
$coordinate = "coords=\"429, 457, 421, 460, 424, 464, 433, 465, 433, 460\"";
$s = preg_replace('/coords\s*=\s*"([^"]+)"/', '$1', $coordinate);
$coordinate = 'coords="' . implode(", ", array_map(function($n) {return $n*2;}, 
               explode(",", $s))) . '"';
echo $coordinate . "\n";
//=> coords="858, 914, 842, 920, 848, 928, 866, 930, 866, 920"