views:

1084

answers:

6

Hi, 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 thats not the most elegant solution.

For example

$sting = "/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

+5  A: 

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.

jheddings
Funny, i just came up with the same solution, there seem to be a lot of different ways to approach this simple problem
andrew
Not surprising that PHP was loosely derived from Perl ;) http://en.wikipedia.org/wiki/There%27s_more_than_one_way_to_do_it
jheddings
But this will fail if '?' is not found in the string, i.e. it will result in an empty string.
nikc
Not sure, but I am guessing two function calls (`substr` and `strpos`) take more resources than a single call like `strstr` or `strtok`. Of course if its just run once per page load, who cares :)
Doug Neiner
might try http://uk3.php.net/manual/en/function.strrpos.php repetitively instead?
warren
@nikc: yeah, I warned you it wasn't very robust ;) @dcneiner: Very true... I didn't realize that third argument had been added to `strstr`. That is probably a better way to go if you have 5.3 or above on your server.
jheddings
+6  A: 
//you could use explode:
$string = "/gallery/image?a=b";
list($url,$querystring) = explode('?', $string, 2);
Rob
+1 least complicated
seanmonstar
Useful, because now i have the query sting in a variable so i can use it later if i need to. I'm all about minimising my code.
andrew
+1 - Neater than my code.
Dominic Rodger
+1  A: 
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.

Dominic Rodger
+2  A: 

The strstr and stristr functions finds the first occurrence in a string and returns everything after it (including the search string). But it you supply true as the third argument, it brings back everything in front of the search string.

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

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

if( $path = strstr( $string, '?', true) ){
   # Do something
}
Doug Neiner
Be careful with `stristr`: "Returns the matched substring. If needle is not found, returns `FALSE`."
Dominic Rodger
You don’t need `stristr`. `strstr` will suffice.
Gumbo
Thanks @Dominic and @Gumbo. Updated my answer to reflect both of your comments.
Doug Neiner
+3  A: 

Try strtok:

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

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

Gumbo
go c stdlib string.h!
warren
A: 

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.

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

Sridhar