首页 > 解决方案 > 如何截断字符串,或从 php 中的字符串中提取部分值

问题描述

你好,我的值是 1284&kec=220107&prop=220000

这里我要取前面的数字&前面 的数字并不总是三个字母,有时两个字母有时也是一个,如何删除或删除&后面的值

example : 1284&kec=220107&prop=220000
result value = 1284

example : 11&kec=2332&prop=563454
result value = 11

能给我举个例子吗?

标签: phpcodeignitersubstr

解决方案


您可以使用explode()将其转换string为an array,然后得到您想要的。像这样——

$example = '1284&kec=220107&prop=220000';
$ex = explode("&kec", $example); // convert it to array where $kec is found
$value = $ex[0]; // output: 1284 // the first key will hold the value before $kec

$example2 ='11&kec=2332&prop=563454';
$ex = explode("&kec", $example2);
$value = $ex[0]; // output: 11

推荐阅读