Php 修剪字符串在特定字符处

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1830992/
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 03:59:12  来源:igfitidea点击:

Php trim string at a particular character

phpstring

提问by andrew

Is there a php string function to trim a string after a particular character. I had a look on the php.net site and did a google search but couldn't find anything. The only solution I could think of was explode and then grab the first part of the array but that's not the most elegant solution.

是否有 php 字符串函数可以在特定字符后修剪字符串。我查看了 php.net 站点并进行了谷歌搜索,但找不到任何内容。我能想到的唯一解决方案是爆炸,然后获取数组的第一部分,但这不是最优雅的解决方案。

For example

例如

$string = "/gallery/image?a=b";
$string = imaginary_function($string,'?');
echo $string; //echoes "/gallery/image"

Does anyone know of a good cheat sheet for php string manipulation functions? Thanks

有谁知道一个很好的 php 字符串操作函数的备忘单?谢谢

回答by jheddings

Maybe something like this:

也许是这样的:

$string = substr($string, 0, strpos($string, '?'));

Note that this isn't very robust (i.e. no error checking, etc), but it might help you solve your problem.

请注意,这不是很健壮(即没有错误检查等),但它可能会帮助您解决问题。

回答by Rob

You could use explode:

你可以使用爆炸

$string = "/gallery/image?a=b";
list($url,$querystring) = explode('?', $string, 2);

回答by Gumbo

Try strtok:

尝试strtok

$token = strtok($str, '?');

Note that the second parameter is a list of separators and not a separator string.

请注意,第二个参数是分隔符列表,而不是分隔符字符串。

回答by Doug Neiner

The strstrand stristrfunctions finds the first occurrence in a string and returns everything after it (including the search string). But it you supply trueas the third argument, it brings back everything in front of the search string.

strstrstristr一个字符串函数找到第一个出现并返回后它的一切(包括搜索字符串)。但是如果您提供true第三个参数,它会带回搜索字符串前面的所有内容。

$string = strstr( $string, '?', true); # Returns /gallery/image

If the match is not found it returns FALSEso you could write an error check like this:

如果未找到匹配项,它将返回,FALSE因此您可以编写如下错误检查:

if( $path = strstr( $string, '?', true) ){
   # Do something
}

回答by Dominic Rodger

function imaginary_function($string, $char) {
  $index = strpos($char, $needle);
  if ($index === false) { return $string };

  return substr($string, 0, $index);
}

An excellent, official list of string manipulation functions is available here.

此处提供一份出色的官方字符串操作函数列表。

回答by Ja?ck

Although pure string functions may give better performance, this is not just a string; it's a URI. Therefore it makes more sense to use a function that's made to handle such data:

虽然纯字符串函数可能会提供更好的性能,但这不仅仅是一个字符串;这是一个URI。因此,使用用于处理此类数据的函数更有意义:

echo parse_url("/gallery/image?a=b", PHP_URL_PATH);
// Output: /gallery/image

回答by Sridhar Balasubramanian

This may be overkill for what you are trying to do, but if you want to break apart a URL into pieces, try the PHP function parse_url. Here's the PHP manual page.

这对于您想要做的事情来说可能有点过分,但是如果您想将 URL 分解成多个部分,请尝试使用 PHP 函数parse_url。这是PHP 手册页

You'd then want the "path" portion of the resulting array.

然后您需要结果数组的“路径”部分。