php - 在php中获取特定值之后的所有字符串
问题描述
我需要所需的输出,无论它是在 preg_match 中还是在不是 probs 的爆炸中。我需要给定数组格式的输出。
我正在尝试从这个变量中拆分字符串
输入
嗨 FF_nm,您的城市是 FF_city,您的性别是 FF_gender。网络研讨会标题是 FF_extraparam1 你的名字又是 FF_nickname
输出
数组 => [昵称,城市,性别,extraparam1,昵称]
代码
<?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;
?>
解决方案
您可以将explode
withforeach
循环用作:
$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...
}
参考:array-shift , explode , preg-replace
推荐阅读
- c++ - C++ 数组下标差异
- python - 确认安装后 Python 3.8.5 “error:_tkinter module not found”
- python - 如何从 Pandas DataFrame 中的重复行中获取列?
- android - 在 ConstraintLayout 中相对于上面的视图居中视图
- javascript - 为元素添加不同的数据属性,由js创建
- django - 在 Django 的基于类的视图中显示消息?
- android - kotlin 中不生成 Dagger 组件
- javascript - Jquery 在悬停时添加数据 id,另一个在悬停时获取数据 id,但它只获取第一个 id
- javascript - 在javascript中将字符串转换为yyyy-dd-mm日期对象
- google-apps-script - 面临授权我创建的 Google 脚本的问题