简体   繁体   English

将带有多个分隔符的格式化字符串解析为关联数组

[英]Parse a formatted string with multiple delimiters into an associative array

I am using a module to create booking orders in my website.我正在使用一个模块在我的网站上创建预订订单。

They have a hook I can use which fires after a new booking is created.他们有一个钩子,我可以使用它在创建新预订后触发。

The variable I get is booking_form: Inside this variable is the following:我得到的变量是booking_form:在这个变量里面是这样的:

text^name1^PETER~text^secondname1^SMITH~text^phone1^023482348~text^adres1^STREETAVENUE 1B~text^postcode1^91201CA~text^woonplaats1^LIVINGPLACE~email^email1^peter@example.com

Is there someway in PHP to get all the values of all the fields in an array or something? PHP 中是否有某种方法可以获取数组中所有字段的所有值? So something like this?所以像这样的事情?

array(
    name or name1? => PETER,
    secondname => SMITH,
    etc... => etc...
)

So the function has to split the string based on the ^ and ~ characters.因此该函数必须根据 ^ 和 ~ 字符拆分字符串。

Try this :尝试这个 :

$result = array();
$data = explode ( "~", $yourString )
foreach ( $data as $element)
{
    $elt = explode ( "^", $element ) ;
    $result[$elt[1]] = $elt[2];
}

Another way to do this...另一种方法来做到这一点......

$input = 'text^name1^PETER~text^secondname1^SMITH~text^phone1^023482348~text^adres1^STREETAVENUE 1B~text^postcode1^91201CA~text^woonplaats1^LIVINGPLACE~email^email1^peter@example.com';

$split = explode('~', $input);
$finalArray = [];

foreach($split as $thing) {
    list ($type, $key, $value) = explode('^', $thing);
    $finalArray[$key] = $value;
}

print_r($finalArray);

Your formatted string seems inconsistently formatted and I don't understand the relevance of the text and email markers.您的格式化字符串的格式似乎不一致,我不明白textemail标记的相关性。

Furthermore, we don't know your input data may vary because you only gave us one and string AND we don't know your exact desired output because you "yatta-yatta"ed what you expect.此外,我们不知道您的输入数据可能会有所不同,因为您只给了我们一个和字符串,而且我们不知道您确切想要的输出,因为您“yatta-yatta”符合您的期望。

That said, parsing the string can be done with regex.也就是说,可以使用正则表达式来解析字符串。

  • Match the text or email label followed by a literal caret匹配文本或电子邮件标签后跟文字插入符号
  • Capture one or more non-caret characters捕获一个或多个非插入字符
  • Match a literal caret匹配文字插入符号
  • Capture zero or more non-caret/non-tilde characters捕获零个或多个非插入符号/非波浪号字符

Code: ( Demo )代码:(演示

$string = 'text^name1^PETER~text^secondname1^SMITH~text^phone1^023482348~text^adres1^STREETAVENUE 1B~text^postcode1^91201CA~text^woonplaats1^LIVINGPLACE~email^email1^peter@example.com';

preg_match_all('/(?:text|email)\^([^^]+)\^([^^~]*)/', $string, $matches, PREG_SET_ORDER);
var_export(
    array_column($matches, 2, 1)
);

Output:输出:

array (
  'name1' => 'PETER',
  'secondname1' => 'SMITH',
  'phone1' => '023482348',
  'adres1' => 'STREETAVENUE 1B',
  'postcode1' => '91201CA',
  'woonplaats1' => 'LIVINGPLACE',
  'email1' => 'peter@example.com',
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过多个分隔符将字符串拆分为关联数组 - Split string into associative array by multiple delimiters 将交替字母和数字(无分隔符)的字符串解析为关联数组 - Parse string of alternating letters and numbers (no delimiters) to associative array 将带有两个分隔符的字符串转换为平面关联数组 - Convert string with two delimiters into flat associative array 将没有分隔符的字符串转换为关联的多维数组 - Convert string with no delimiters into associative multidimensional array 正则表达式将格式化的字符串拆分为关联数组 - regular expression to split formatted string into associative array 解析包含 3 个分隔符的格式化字符串以创建多个平面 arrays - Parse formatted strings containing 3 delimiters to create multiple flat arrays 将定界字符串解析为关联数组 - parse delimited string into associative array PHP:将字符串[“ key”,“ value”]解析为关联数组 - PHP: Parse string [“key”, “value”] into associative array 使用 = 和 & 作为分隔符解析字符串,但并非所有 & 都是向前看的分隔符 - Parse string with = and & as delimiters, but not all & are delimiters with look ahead 如何使用preg_match_all()将带有2个分隔符的字符串转换为关联数组? - How can I use preg_match_all() to convert a string with 2 delimiters into an associative array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM