php 从字符串中删除第一个和最后一个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3690470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:43:18  来源:igfitidea点击:

Remove first and last char from string

phptrim

提问by user248488

I have this:

我有这个:

$dataList = "*one*two*three*";
$list = explode("*", $dataList);
echo"<pre>";print_r($list);echo"</pre>";

which outputs:

输出:

> Array (
>     [0] => 
>     [1] => one
>     [2] => two
>     [3] => three
>     [4] =>  )

How do I strip the fist and last * in the string before exploding?

如何在爆炸前去除字符串中的拳头和最后一个 *?

回答by NikiC

Using trim:

使用trim

trim($dataList, '*');

This will remove all *characters (even if there are more than one!) from the end and the beginning of the string.

这将从*字符串的末尾和开头删除所有字符(即使有多个!)。

回答by Bojoer

Some other possibilities:

其他一些可能性:

Using substr:

使用 substr:

$dataList = substr($dataList, 1, -1);

You can also choose to not remove the * from the string but rather remove the empty array values which will always be the first and last element. Using array functions array_pop() and array_shift():

您还可以选择不从字符串中删除 *,而是删除始终是第一个和最后一个元素的空数组值。使用数组函数 array_pop() 和 array_shift():

$arrData = array_pop(array_shift($arrData));

回答by reko_t

trim($dataList, "*")

回答by Syed Sxxis

$string = substr($dataList, 1, -1);

$string = substr($dataList, 1, -1);

Remove the first and the last character of the string in php

去掉php中字符串的第一个和最后一个字符

回答by Akbar Adeeb

echo trim($dataList,"*");

hope this solve your problem

希望这能解决你的问题