1

This may be a dupe, but I cannot seem to find a thread which matches this issue. I want to remove all chars from a string after a given sub-string - but the chars and the number of chars after the sub-string is unknown. Most solutions I have found seem to only work for removing the given sub-string itself or a fixed length after a given sub-string.

I have

$str = preg_replace('(.gif*)','.gif$',$str);

Which locates 'blahblah.gif?12345' ok, but I cannot seem to remove the chars after the sub-string '.gif'. I read that $ denotes EOS so I thought this would work, but apparently not. I also tried

'.gif$/' 

and simply

'.gif'
3
  • Are you only dealing with URLs? Is the sole purpose of this to remove query parameters? If this is the case there are easier (non-regex) methods that you can use.. Commented Jul 28, 2014 at 15:53
  • I'm dealing with images, so if I used parse_url would www.page.com/image.gif?123 not simply return www.page.com? Commented Jul 28, 2014 at 16:01
  • 1
    Actually, no. It'll return exactly what you are looking for - the URL without the query string (everything after the ? character). Commented Jul 28, 2014 at 17:35

4 Answers 4

1

It can be done without regex:

echo substr('blahblah.gif?12345', strpos('blahblah.gif?12345', '.gif') + 4);
// returns ?12345                    this is the length of the substring ^

So the code is:

$str = 'original string';
$match = 'matching string';
$output = substr($str, strpos($str, $match) + strlen($match));

Ok, now I'm not sure if you want to keep the first or the second part of the string. Anyway, here's the code for keeping the first part:

echo substr('blahblah.gif?12345', 0, strpos('blahblah.gif?12345', '.gif') + 4);
// returns blahblah.gif           ^ this is the key

And the full code:

$str = 'original string';
$match = 'matching string';
$output = substr($str, 0, strpos($str, $match) + strlen($match));

See the both examples work here: http://ideone.com/Ge30rY

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for effort alone. The examples you gave helped immensely, thanks.
I'm glad I could help, you're welcome. I'll try reading questions more carefully in the future :)
1

Assuming (from OP's comment) that you are working with actual URLs as your source string, I believe that the best course of action here would be to use PHP's built-in functionality for working with and parsing URLs. You do this by using the parse_url() function:

(PHP 4, PHP 5)
parse_url — Parse a URL and return its components

This function parses a URL and returns an associative array containing any of the various components of the URL that are present.

This function is not meant to validate the given URL, it only breaks it up into the above listed parts. Partial URLs are also accepted, parse_url() tries its best to parse them correctly.

From your example: www.page.com/image.gif?123 (or even just image.gif?123) using parse_url() will look something like this:

var_dump( parse_url( "www.page.com/image.gif?123" ) );

array(2) {
  ["path"]=>
  string(22) "www.page.com/image.gif"
  ["query"]=>
  string(3) "123"
}

As you can see, without the need for regular expressions or string manipulations we have broken up the URL into it's separate components. No need to re-invent the wheel. Nice and clean :)

1 Comment

That indeed is a good clean implementation. I will remember that in future, thanks.
0

You could do this:

$str = "somecontent.gif?anddata";
$pattern = ".gif";
echo strstr($str,$pattern,true).$pattern;

Comments

0
// Set up string to search through
$haystack = "blahblah.gif?12345";

// Determine substring and length of it
$needle = ".gif";
$length = strlen($needle);

// Find position of last substring
$location = strrpos($haystack, $needle);

// Use location of last occurence + it's length to get new string
$newtext = substr($haystack, 0, $location+$length);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.