hi andras-

consider the strstr() and strrstr() functions :

  $str = 'abcabc';

    print strpos ($str,'b'); // 1
    print strrpos($str,'b'); // 4

these positions can be used with other functions, such as substr() :

  $str = 'abc.png';

    print strpos($str,'.');                // 3
    print substr($str,strpos($str,'.'));   // .png
    print substr($str,strpos($str,'.')+1); // png  
    print substr($str,0,strpos($str,'.')); // abc

and it's good to keep in mind that foo.tar.gz and foo.bar.eh.boo may give
unexpected results so choose your logic wisely :)

and as already stated, split() and explode() would work too :

  $str = 'abc.png';

    $pieces = explode('.',$str);

    print $pieces[0]; // abc
    print $pieces[1]; // png

also, depending on what you're doing, something like the following may be
appropriate as it'll check the end of $str for a desired ending :

  if (eregi('(gif|jpg|png|jpeg)$',$str)) {
    print 'yes, variable $str ends with gif, jpg, png or jpeg';
  }

warm regards,
Philip Olson

On Thu, 20 Sep 2001, Andras Kende wrote:

> Hi,
> 
> I trying to cut the last 4 char of a string but sometimes its cut 3 sometime
> 4 char....
> 
> $pic1=$amyrow["picture"];
> $pic2 = substr("$pic1", 0, -4);
> 
> Actually I have some pictures where I need to cut off the extensions...
> 
> amamm.jpg
> 33.jpg
> 321.gif
> 
> to
> 
> amamm
> 33
> 321
> 
> Is any other way than substr to do this ??
> 
> Thanks
> 
> Andras
> 
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: [EMAIL PROTECTED]
> 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to