如何从字符串获得子字符串在Php
问题描述:
我有一个像如何从字符串获得子字符串在Php
$totalValues ="4565:4,4566:5,4567:6";
现在字符串我想':'
昏迷分离后仅值,即我想串像$SubValues = "4,5,6"
感谢
答
$totalValues ="4565:4,4566:5,4567:6";
echo implode(',',array_map(function($val){
$val = explode(':',$val);
return $val[1];
},explode(',',$totalValues)));
答
试试这个代码,它正在
<?php
$str="4565:4,4566:5,4567:6";;
$data =explode(",", $str);
echo '<pre>';
print_r($data);
$newstring="";
foreach ($data as $key => $value) {
preg_match('/:(.*)/',$value,$matches);
$newstring.=$matches[1].",";
}
echo rtrim($newstring,",");
我已经更新了我的代码,请检查一下
<?php
$str="4565:4,4566:5,4567:6";
$data1=explode(",", $str);
$newstring1="";
foreach ($data1 as $key1 => $value) {
preg_match('/(.*?):/',$value,$matches);
$newstring1.=$matches[1].",";
}
echo rtrim($newstring1,",");
+0
谢谢@Sharma Vikram,现在我想要“4565,4566,4567”字符串,请帮助我 – Shekkar
+0
请检查它 –
答
你可能想这样说:
$totalValues ="4565:4,4566:5,4567:6";
$temp = explode(':', $totalValues);
$subValues = $temp[1][0];
$x = count($temp);
for ($i = 2; $i < $x; $i++) {
$subValues .= ',' . $temp[$i][0];
}
echo $subValues;
为什么人们赋予下坡实时开发问题 – Shekkar
您的问题有没有试图尝试的迹象也没有研究工作 – Andrew