首页 > 解决方案 > 在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; 

    ?>

标签: phparrayspreg-replacepreg-match

解决方案


您可以将explodewithforeach循环用作:

$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

现场示例:https ://3v4l.org/Oa1sn


推荐阅读