Php 在多个单词上分解字符串

Php 在多个单词上分解字符串,php,arrays,regex,explode,Php,Arrays,Regex,Explode,有这样一个字符串: $string = 'connector:rtp-monthly direction:outbound message:error writing data: xxxx yyyy zzzz date:2015-11-02 10:20:30'; $desired_result = array( 'connector' => 'rtp-monthly', 'direction' => 'outbound', 'message' => '

有这样一个字符串:

$string = 'connector:rtp-monthly direction:outbound message:error writing data: xxxx yyyy zzzz date:2015-11-02 10:20:30';
$desired_result = array(
    'connector' => 'rtp-monthly',
    'direction' => 'outbound',
    'message' => 'error writing data: xxxx yyyy zzzz',
    'date' => '2015-11-02 10:20:30',
);
此字符串来自用户输入。因此,它将永远不会有相同的顺序。这是一个输入字段,我需要拆分它来构建DB查询

现在,我想根据数组()中给定的单词拆分字符串,数组()类似于包含我需要在字符串中查找的单词的映射器。看起来是这样的:

$mapper = array(
    'connector' => array('type' => 'string'),
    'direction' => array('type' => 'string'),
    'message' => array('type' => 'string'),
    'date' => array('type' => 'date'),
);
只有
$mapper
的键才相关。我用foreach试过,结果爆炸了:

 $parts = explode(':', $string);
但问题是:字符串中可能有冒号,所以我不需要在那里爆炸。我只需要在mapper键后面紧跟冒号时爆炸。本例中的映射器键为:

connector    // in this case split if "connector:" is found
direction    // untill "direction:" is found
message      // untill "message:" is found
date         // untill "date:" is found
但也请记住,用户输入可能会发生变化。因此,字符串将始终更改字符串的顺序,
mapper array()。所以我不确定explode是不是正确的方法,或者我是否应该使用正则表达式。如果是的话,怎么做

所需的结果应该是一个如下所示的数组:

$string = 'connector:rtp-monthly direction:outbound message:error writing data: xxxx yyyy zzzz date:2015-11-02 10:20:30';
$desired_result = array(
    'connector' => 'rtp-monthly',
    'direction' => 'outbound',
    'message' => 'error writing data: xxxx yyyy zzzz',
    'date' => '2015-11-02 10:20:30',
);

非常感谢您的帮助。

我们的目标是创建一个数组,其中包含从字符串中提取的两个数组的值。有两个数组是必要的,因为我们希望考虑两个字符串分隔符。 试试这个:

$parts = array();
$large_parts = explode(" ", $string);

for($i=0; $i<count($large_parts); $i++){
    $small_parts = explode(":", $large_parts[$i]);
    $parts[$small_parts[0]] = $small_parts[1];
}
$parts=array();
$large_parts=分解(“,$string);

对于($i=0;$i)可以使用正则表达式和<代码>开发()/<代码>的组合。请考虑以下代码:

$str = "connector:rtp-monthly direction:outbound message:error writing data date:2015-11-02";
$regex = "/([^:\s]+):(\S+)/i";
// first group: match any character except ':' and whitespaces
// delimiter: ':'
// second group: match any character which is not a whitespace
// will not match writing and data
preg_match_all($regex, $str, $matches);
$mapper = array();
foreach ($matches[0] as $match) {
    list($key, $value) = explode(':', $match);
    $mapper[$key][] = $value;
}

此外,您可能希望首先考虑一种更好的方法来存储字符串(JSON?XML?)。

给您。正则表达式用于“捕获”键(任何字符序列,不包括空格和“:”)。从这里开始,我使用“分解”来“递归”拆分字符串。测试的ad效果良好

$string = 'connector:rtp-monthly direction:outbound message:error writing data date:2015-11-02';

$element = "(.*?):";
preg_match_all( "/([^\s:]*?):/", $string, $matches);
$result = array();
$keys = array();
$values = array();
$counter = 0;
foreach( $matches[0] as $id => $match ) {
    $exploded = explode( $matches[ 0 ][ $id ], $string );
    $keys[ $counter ] = $matches[ 1 ][ $id ];
    if( $counter > 0 ) {
        $values[ $counter - 1 ] = $exploded[ 0 ];
    }
    $string = $exploded[ 1 ];
    $counter++;
}
$values[] = $string;
$result = array();
foreach( $keys as $id => $key ) {
    $result[ $key ] = $values[ $id ];
}
print_r( $result );

其中比较棘手的部分是匹配原始字符串。您可以在以下帮助下使用正则表达式进行匹配:

在这个正则表达式中,
/(连接器|方向|消息|日期):(.+?)(?=连接器:|方向:|消息:|日期:|$)/
,您正在匹配:

  • (连接器|方向|消息|日期)
    -查找关键字并捕获它
  • -后跟冒号
  • (.+?)
    -后跟任意字符多次非贪婪,并捕获它
  • (?=connector:| direction:| message:| date:|$)
    -直到下一个关键字或字符串的结尾,使用非捕获前瞻肯定断言
结果是:

Array
(
    [connector] => rtp-monthly
    [direction] => outbound
    [message] => error writing data: xxxx yyyy zzzz
    [date] => 2015-11-02 10:20:30
)
我使用mapper数组并不是为了让示例更清晰,但是您可以使用
内爆
将关键字组合在一起。

在PHP中使用preg_split()通过多个分隔符分解()

这里只是一个简短的说明。要在PHP中使用多个分隔符分解字符串,您必须使用正则表达式。使用管道字符分隔分隔符

$string = 'connector:rtp-monthly direction:outbound message:error writing data: xxxx yyyy zzzz date:2015-11-02 10:20:30';
$chunks = preg_split('/(connector|direction|message)/',$string,-1, PREG_SPLIT_NO_EMPTY);

// Print_r to check response output.
echo '<pre>';
print_r($chunks);
echo '</pre>';
$string='连接器:rtp每月方向:出站消息:错误写入数据:xxxx yyyy ZZZ日期:2015-11-02 10:20:30';
$chunks=preg_split(“/(连接器方向消息)/”,$string,-1,preg_split_NO_EMPTY);
//打印以检查响应输出。
回声';
打印(块);
回声';

PREG_SPLIT_NO_EMPTY–只返回非空片段。

按空格和按
分割:
使用regexp完成后,您是否有可能将该字符串更改为更易于解析的格式(如json等)?
$result=array_列(array_映射(函数($v){return explode(“:”,$v)},explode(“,$string)),1,0)
/([^:\s]+):(\s+/
两个捕获组,一个在冒号之前,一个在冒号之后。使用
preg\u match\u all()
此外。
编写
数据
是否被忽略?这意味着你无法控制格式…这太糟糕了,考虑到下面所有的答案都需要做大量的工作:虽然这个代码片段可以解决问题,但代码之外确实有助于提高你的文章质量。记住,你是回答未来读者的问题,那些人可能不知道你的代码建议的原因。也请尽量不要用解释性的注释来填充你的代码,这降低了代码和解释的可读性。@麻烦零。谢谢你的回答。这对我没有帮助。考虑我可以得到一个STRI。ng包含一个日期值,如
date:2015-11-02 08:10:15
,因此我确实需要并希望拆分该映射数组键…对于日期部分,您可以首先将“:”替换为“-”或“/”为避免混淆格式。谢谢!请查看有问题的更新字符串:
$string='连接器:rtp每月方向:出站消息:错误写入数据:xxxx日期:2015-11-02 10:20:30';
我想我真的需要在给定的
mapper array()
如何插入
$mapper=array()的键
$pattern
中?我不清楚此
$mapper
数组的用途。如果您可以编辑您的问题并澄清预期的行为,这会有所帮助。此外,您更新的字符串会破坏此正则表达式,因为您同时将分隔符(冒号)用作数据的一部分(时间部分)。如果可能的话,我强烈建议您将输入格式更改为类似JSON的格式。问题已更新,现在更有意义。我知道,分隔符,这是我问题的一部分。这就是为什么我想使用这些“映射词”在delimiter前面。或者delimiter与那些映射词的组合…我已经编辑了我的答案,我认为它达到了您现在需要的。