无论是preg_match还是explode不是问题,我都需要所需的输出。我需要以给定的数组格式输出。
我正在尝试从此变量中拆分字符串
输入
嗨,FF_nm, 您的城市是FF_city,您的性别是FF_gender。网络研讨会的标题是FF_extraparam1 同样,您的名字是FF_nickname
输出
array => [昵称,城市,性别,额外参数1,昵称]
代码
<?php
$data = "Hi FF_nm,
Your city is FF_city and your gender is FF_gender. webinar title is FF_extraparam1
Again your name is FF_nickname";
if (($pos = strpos($data, "FIELDMERGE_")) !== FALSE) {
$whatIWant = substr($data, $pos+1);
}
echo $whatIWant;
?>
答案 0 :(得分:1)
您可以将explode
与foreach
循环一起使用:
$arr = explode("FIELDMERGE_", $data);
array_shift($arr); // you don't need the first element
foreach($arr as $e) {
$word = array_shift(explode(" ", $e)); //take the first word
$res[] = preg_replace('/[^a-z\d]/i', '', $word); //remove the , / . / \n and so on...
}
答案 1 :(得分:0)
您可以将preg_match_all
用于正向后看模式。
preg_match_all('/(?<=FF_)\w+/', $data, $fields);
$output = $fields[0];