如何处理这个字符串并从中提取数据
how to handle this string and extract data out of it
$string='eDreams 459 Travel Up 464 AirAfrica 464 Available at 459 Available at 464 Available at 464 Available at 473 Available at 478 Available at 479 Available at 498';
how to get data like:
array =
0 => string 'eDreams' (length=6)
1 => string '459'
array=
0 => string 'Travel Up' (length=6)
1 => string '464'
array =
0 => string 'AirAfrica' (length=6)
1 => string '464'
怎么做我用爆炸试过了它工作正常但不准确请帮助我?
您想要隔离到单独数组中的元素似乎由多个 space 或制表符分隔,并且数组元素本身由单个 space 分隔。如果字符串实际上采用这种格式,您可以使用 split()
函数将这些元素拉入一个或多个数组。例如:
$split_pattern = " ";
$array = split($split_pattern, $string);
这将生成如下所示的数组:
array =
0 => string 'eDreams 459'
1 => string 'Travel Up 464'
...
4 => string 'Available at'
5 => string '459'
etc...
您将需要再次 split()
每个数组元素以创建格式为 0 => name, 1 => number
的数组。
要处理 available at
条目,您可以使用 array_keys()
函数:
$pattern = "Available at";
$availables = array_keys($array, $pattern);
foreach ($availables as $available){
$availableAt = $array[$available];
$availableNum = $array[$available + 1];
}
那么 $availableAt
将始终是 "Available at"
,但 $availableNum
将包含数字,它只是您最初 split()
数组中的下一个元素。有几种方法可以完成此操作,但我认为这是最清楚的。
参考文献:
我找到了一个方法..它并不漂亮。希望对你有帮助。
<?php
$string = "eDreams 459 Travel Up 464 AirAfrica 464 Available at 459 Available at 464 Available at 464 Available at 473 Available at 478 Available at 479 Available at 498";
$array = explode(" ", $string);
$final_array = array();
$found = false;
foreach ($array as $value) {
if (ctype_digit($value)){
$final_array[count($final_array)-1][] = (int)$value;
$found = false;
} else {
if ($found == false){
$final_array[] = array($value);
$found = true;
} else {
$final_array[count($final_array)-1][0] .= " " . $value;
}
}
}
print_r($final_array);
?>
我不会用这样的东西。也许你可以直接从数据库中得到更好的结果。
我想到了这个解决方案...
$string = 'eDreams 459 Travel Up 464 AirAfrica 464 Available at 459 Available at 464 Available at 464 Available at 473 Available at 478 Available at 479 Available at 498';
$arr = [ ];
$name = '';
$number = '';
$count = strlen( $string );
$reset = 0;
for ( $i = 0; $i < $count; $i++ )
{
if ( is_numeric( $string[ $i ] ) )
{
$reset = 1;
$number.= $string[ $i ];
}
else
{
if ( $reset == 1 )
{
$arr[] = [ trim( $name ), trim( $number ) ];
$name = '';
$number = '';
$reset = 0;
}
else
{
$name.= $string[ $i ];
}
}
}
print_r( $arr );
试试这个:
$string = "eDreams 459 Travel Up 464 AirAfrica 464 Available at 459 Available at 464 Available at 464 Available at 473 Available at 478 Available at 479 Available at 498";
$array = explode(" ", $string);
$final = [];
$str= [];
$num =[];
$temp = $array;
foreach($temp as $k=> $v){
if(!empty($v)){
if(is_numeric($v)){
$num[] = $v;
$final_array[] =[
implode(" ",$str),implode(" ",$num)
];
$str= [];
$num =[];
}else{
$str[] = $v;
}
$temp[$k] = "";
}
}
print_r($final);
$string='eDreams 459 Travel Up 464 AirAfrica 464 Available at 459 Available at 464 Available at 464 Available at 473 Available at 478 Available at 479 Available at 498';
how to get data like:
array =
0 => string 'eDreams' (length=6)
1 => string '459'
array=
0 => string 'Travel Up' (length=6)
1 => string '464'
array =
0 => string 'AirAfrica' (length=6)
1 => string '464'
怎么做我用爆炸试过了它工作正常但不准确请帮助我?
您想要隔离到单独数组中的元素似乎由多个 space 或制表符分隔,并且数组元素本身由单个 space 分隔。如果字符串实际上采用这种格式,您可以使用 split()
函数将这些元素拉入一个或多个数组。例如:
$split_pattern = " ";
$array = split($split_pattern, $string);
这将生成如下所示的数组:
array =
0 => string 'eDreams 459'
1 => string 'Travel Up 464'
...
4 => string 'Available at'
5 => string '459'
etc...
您将需要再次 split()
每个数组元素以创建格式为 0 => name, 1 => number
的数组。
要处理 available at
条目,您可以使用 array_keys()
函数:
$pattern = "Available at";
$availables = array_keys($array, $pattern);
foreach ($availables as $available){
$availableAt = $array[$available];
$availableNum = $array[$available + 1];
}
那么 $availableAt
将始终是 "Available at"
,但 $availableNum
将包含数字,它只是您最初 split()
数组中的下一个元素。有几种方法可以完成此操作,但我认为这是最清楚的。
参考文献:
我找到了一个方法..它并不漂亮。希望对你有帮助。
<?php
$string = "eDreams 459 Travel Up 464 AirAfrica 464 Available at 459 Available at 464 Available at 464 Available at 473 Available at 478 Available at 479 Available at 498";
$array = explode(" ", $string);
$final_array = array();
$found = false;
foreach ($array as $value) {
if (ctype_digit($value)){
$final_array[count($final_array)-1][] = (int)$value;
$found = false;
} else {
if ($found == false){
$final_array[] = array($value);
$found = true;
} else {
$final_array[count($final_array)-1][0] .= " " . $value;
}
}
}
print_r($final_array);
?>
我不会用这样的东西。也许你可以直接从数据库中得到更好的结果。
我想到了这个解决方案...
$string = 'eDreams 459 Travel Up 464 AirAfrica 464 Available at 459 Available at 464 Available at 464 Available at 473 Available at 478 Available at 479 Available at 498';
$arr = [ ];
$name = '';
$number = '';
$count = strlen( $string );
$reset = 0;
for ( $i = 0; $i < $count; $i++ )
{
if ( is_numeric( $string[ $i ] ) )
{
$reset = 1;
$number.= $string[ $i ];
}
else
{
if ( $reset == 1 )
{
$arr[] = [ trim( $name ), trim( $number ) ];
$name = '';
$number = '';
$reset = 0;
}
else
{
$name.= $string[ $i ];
}
}
}
print_r( $arr );
试试这个:
$string = "eDreams 459 Travel Up 464 AirAfrica 464 Available at 459 Available at 464 Available at 464 Available at 473 Available at 478 Available at 479 Available at 498";
$array = explode(" ", $string);
$final = [];
$str= [];
$num =[];
$temp = $array;
foreach($temp as $k=> $v){
if(!empty($v)){
if(is_numeric($v)){
$num[] = $v;
$final_array[] =[
implode(" ",$str),implode(" ",$num)
];
$str= [];
$num =[];
}else{
$str[] = $v;
}
$temp[$k] = "";
}
}
print_r($final);