String manipulation in PHP 5

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
buttsp
Forum Newbie
Posts: 13
Joined: Thu May 11, 2006 9:17 am

String manipulation in PHP 5

Post by buttsp »

I have the following header:

HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Expires: -1 Pragma: no-cache Cache-Control: no-cache Set-Cookie: JSESSIONID=74E66481C486668AA1DDDEED2F7A37A8; Path=/travelsearch Content-Type: text/xml;charset=ISO-8859-1 Content-Length: 97 Date: Thu, 11 May 2006 14:01:28 GMT OK

-------------

And I want to extract a substring starting from the begining till *GMT* at the end, but leavign out the last *OK*.

PS: There are two Oks in the entire string. i'm new to php5 so not very familiar with the api. :p
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post by ambivalent »

substr() should do what you need.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You actually would be better using something like preg_match() since substr() only works on fixed points in the string. If you were to go with substr() you'd need to combine it with strpos() to get the positions of OK and GMT.
buttsp
Forum Newbie
Posts: 13
Joined: Thu May 11, 2006 9:17 am

Post by buttsp »

Yes you are right, the simple strstr doesnt work, it works only for single character. I have solved the problem though

public function strring_strrpos($haystack,$needle){
while($return = strrpos($haystack,$needle))
{
if(strncmp(substr($haystack,$return,strlen($needle)),
$needle,strlen($needle)) == 0 )
return $return;
$haystack = substr($haystack,0,$return -1 );
}
return $return;
}
Post Reply