PHP - get certain word from string

不想你离开。 提交于 2019-12-01 09:02:37

问题


If i have a string like this:

$myString = "input/name/something";

How can i get the name to be echoed? Every string looks like that except that name and something could be different.


回答1:


so the only thing you know is that :

  • it starts after input
  • it separated with forward slashes.

>

$strArray = explode('/',$myString);
$name = $strArray[1];
$something = $strArray[2];



回答2:


If you only need "name"

list(, $name, ) = explode('/', $myString);
echo "name is '$name'";

If you want all, then

list($input, $name, $something) = explode('/', $myString);



回答3:


Try this:

$parts = explode('/', $myString);
echo $parts[1];

This will split your string at the slashes and return an array of the parts. Part 1 is the name.




回答4:


use the function explode('/') to get an array of array('input', 'name', 'something'). I'm not sure if you mean you have to detect which element is the one you want, but if it's just the second of three, then use that.



来源:https://stackoverflow.com/questions/1415210/php-get-certain-word-from-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!