Page 1 of 1

String manipulation in PHP 5

Posted: Thu May 11, 2006 9:23 am
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

Posted: Thu May 11, 2006 9:29 am
by ambivalent
substr() should do what you need.

Posted: Thu May 11, 2006 9:37 am
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.

Posted: Thu May 11, 2006 9:42 am
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;
}