Php 从字符串中获取特定内容
我需要从给定的字符串中获取作为数组的数字 示例字符串:Php 从字符串中获取特定内容,php,string-matching,Php,String Matching,我需要从给定的字符串中获取作为数组的数字 示例字符串: $t = '1-P,2-T,3-P,4-R,5-C,6-T,'; 预期产出: 如果我搜索-T,输出需要如下: 我尝试了var_导出(explode(“-T,”,$T))但它没有按预期工作。 有人能给我一个建议吗?试试这个: $t = '211111111131-P,2-T,3654554-P,4-R,5-C,6-T,'; $find = "-P"; // Search element $found = [];
$t = '1-P,2-T,3-P,4-R,5-C,6-T,';
预期产出:
- 如果我搜索
,输出需要如下:-T
我尝试了
var_导出(explode(“-T,”,$T))代码>但它没有按预期工作。
有人能给我一个建议吗?试试这个:
$t = '211111111131-P,2-T,3654554-P,4-R,5-C,6-T,';
$find = "-P"; // Search element
$found = []; // Result array
$array = explode(",", $t); // Breaking up into array
foreach($array as $arr) {
if (strpos($arr, $find)) { // Checking if search element is found in $arr
$found[] = explode('-',$arr)[0]; // Extracting the number prefix e.g 1 for 1-P
}
}
输出:
Array
(
[0] => 1
[1] => 3
)
把它当作
$t = '1-P,2-T,3-P,4-R,5-C,6-T,';
$data = explode(",", $t);
print_r($data);
$row=array();
for ($i = 0; $i <= count($data); $i++) {
if (!empty($data[$i])) {
if (strpos($data[$i], '-T') !== false) {// pass find value here
$final = explode("-", $data[$i]);
$row[]=$final[0];
}
}
}
print_r($row);
以下匹配搜索词-p
前面的完整整数
让我们保持简洁:
$matches = array();
if (preg_match_all('/([0-9]+)\-P/', $t, $matches) >= 1) {
var_dump($matches[1]);
}
搜索'/([0-9]+)\-p/
,'/([0-9]+)\-C/
,'/([0-9]+)\-T/
等等
查找不同搜索词/过滤器的更动态的方法:
$filter = '-T';
$pattern = sprintf('/([0-9]+)%s/', preg_quote($filter));
请参阅和函数。这里已经有很多很好的答案,但没有一个采用先将数据放入更好的结构的方法
下面的代码将数据转换为关联数组,将字母映射到数字数组,这样您就可以按所需字母重复查找:
$t = '1-P,2-T,3-P,4-R,5-C,6-T,';
$a = array_filter(explode(',', $t));
$map = [];
foreach($a as $item) {
$exploded = explode('-', $item);
$number = $exploded[0];
$letter = $exploded[1];
if (!array_key_exists($letter, $map)) {
$map[$letter] = [];
}
$map[$letter][] = $number;
}
print_r($map);
// Array
// (
// [P] => Array
// (
// [0] => 1
// [1] => 3
// )
//
// [T] => Array
// (
// [0] => 2
// [1] => 6
// )
//
// [R] => Array
// (
// [0] => 4
// )
//
// [C] => Array
// (
// [0] => 5
// )
//
// )
print_r($map['T']);
// Array
// (
// [0] => 2
// [1] => 6
// )
print_r($map['P']);
// Array
// (
// [0] => 1
// [1] => 3
// )
为什么它只为P
返回3
,而不为1
返回?毕竟,你有3-P
和1-P
我只是在等怀孕狂XD。这个解决方案很好,因为它也能找到长度大于1的数字。您可以使用一个变量使其工作,在该变量中,op可以定义要搜索的内容(-P
/-T
等)。我不知道preg/preg_函数。但想法就在那里;)这是最好的解决方案你的答案是正确的,但如果它是这样的$t='2111111131-P,2-t,3654554-P,4-R,5-C,6-t';不工作它只返回第一个元素。@KuppurajShanmugam“不工作”是什么意思-
$t = '1-P,2-T,3-P,4-R,5-C,6-T,';
$temp = [];
// if the last comma is not typo the 3rd argument `-1` omit empty item
$array = explode(",", $t, -1);
foreach($array as $arr) {
list($v, $k) = explode('-', $arr);
$temp[$k][] = $v;
}
print_r($temp['T']);
$matches = array();
if (preg_match_all('/([0-9]+)\-P/', $t, $matches) >= 1) {
var_dump($matches[1]);
}
$filter = '-T';
$pattern = sprintf('/([0-9]+)%s/', preg_quote($filter));
$t = '1-P,2-T,3-P,4-R,5-C,6-T,';
$a = array_filter(explode(',', $t));
$map = [];
foreach($a as $item) {
$exploded = explode('-', $item);
$number = $exploded[0];
$letter = $exploded[1];
if (!array_key_exists($letter, $map)) {
$map[$letter] = [];
}
$map[$letter][] = $number;
}
print_r($map);
// Array
// (
// [P] => Array
// (
// [0] => 1
// [1] => 3
// )
//
// [T] => Array
// (
// [0] => 2
// [1] => 6
// )
//
// [R] => Array
// (
// [0] => 4
// )
//
// [C] => Array
// (
// [0] => 5
// )
//
// )
print_r($map['T']);
// Array
// (
// [0] => 2
// [1] => 6
// )
print_r($map['P']);
// Array
// (
// [0] => 1
// [1] => 3
// )