Page 1 of 2

need to get only the numbers from this string

Posted: Fri Jun 10, 2005 12:19 am
by malcolmboston
hey everyone,

i basically need to find out the version number of the GD version im using, i currently have this in an object

Code: Select all

bundled (2.0.28 compatible)
i need to get only the number so i can do something to the affect of

Code: Select all

if ($version >= 2)
{
   // use advanced features
}
else
{
   // use crap features
}
i think the string might change (like no "bundled") in it, can anyone help me out?

Posted: Fri Jun 10, 2005 12:49 am
by malcolmboston
i came up with this...

Code: Select all

$string = "bundled (2.0.28 compatible)";
preg_match_all('/(?:([0-9]+)|.)/i', $string, $matches);
$count = count($matches[1]);
$version = "";
for ($i = 1; $i <= $count; $i++)
{
	if (!isset($matches[1][$i]))
	{
		//
	}
	else
	{
		$version .= "{$matches[1][$i]}";
	}
}
output 2028, i can use this and just check theres a value higher than 2000, whats the more efficient way?

Posted: Fri Jun 10, 2005 12:53 am
by anjanesh
I thought there was an easier method to return 2.0.28

Code: Select all

<?php
$str = "bundled (2.0.28 compatible)";
$pattern = "/^.*?((\d|\.)*?).*?$/i";
preg_match($pattern,$str,$matches);
print_r($matches);
?>
This pattern string is not correct.

Posted: Fri Jun 10, 2005 1:07 am
by malcolmboston
just thought of another problem..
im current using this code

Code: Select all

if ($this->env->gdversionnum >= 2010) // v2
            {
            	// cool, we can use the good functions
            	$temp = imagecreatetruecolor($this->thumbnail->width, $this->thumbnail->height) or die (OutputError ($message = "Thumb Creation Failed!"));
            	imagecopyresampled($temp, imagecreatefromjpeg($this->image->filepath), 0, 0, 0, 0, $this->thumbnail->width, $this->thumbnail->height, $this->image->width, $this->image->height);
            }
            else
            {
            	// crap functions coming up...
            	$temp = imagecreate($this->thumbnail->width, $this->thumbnail->height) or die (OutputError ($message = "Thumb Creation Failed!"));
            	imagecopyresized($temp, imagecreatefromjpeg($this->image->filepath), 0, 0, 0, 0, $this->thumbnail->width, $this->thumbnail->height, $this->image->width, $this->image->height);
            }
however what if they were using 2.0.1.0, im assuming GD would class its version as 2.0.1 therefor failing my statement, i could add an or statement for 201 but from there, its all gonna go downhill

Posted: Fri Jun 10, 2005 1:29 am
by anjanesh
If you can extract 2.0.28 then you can get it as Major Ver(2), Minor Ver(0), Update Ver(28 ).

Posted: Fri Jun 10, 2005 2:55 am
by malcolmboston
any ideas on the correct preg call then?

Posted: Fri Jun 10, 2005 3:43 am
by Chris Corbyn

Code: Select all

$string = "bundled (2.0.28 compatible)";
preg_match('/\((\d+)\.(\d+)\.(\d+)[^\)]*\)/', $string, $matches);
print_r($matches);

Posted: Fri Jun 10, 2005 3:45 am
by malcolmboston
thank you d11, your a regex legend

as a side note, how did you learn regex? like what books did you use as i feel i need to get upto your level to be honest

Thanks again, Mal

Posted: Fri Jun 10, 2005 4:03 am
by Chris Corbyn
malcolmboston wrote:thank you d11, your a regex legend

as a side note, how did you learn regex? like what books did you use as i feel i need to get upto your level to be honest

Thanks again, Mal
:oops: LOL

I have never picked a book up on regex. I have no idea how I picked it up so quickly. It all just makes logical sense to me.

What happened was this. My ex was at Uni doing Computer science and had to learn Perl. She really couldn't figure it out one little bit so I, being quite into my programming decided to learn Perl and teach it to her in a way she could understand :P

Perl is pretty heavy on regex (and the course focussed on them alot). My learning has mostly come from the perl website.

http://www.perl.com/doc/manual/html/pod/perlre.html (Beware it's not that friendly)

But in all honesty the best way, as with many things is just to practise, practise and practise....

It's not about learning how all the syntax etc, it's about learning how to think about them.

On a side note: Would people find it helpful if I put a kind of Perl primer quiz on the board or something. Like a set of questions starting easy and getting more advanced so that you can learn how to think about regex?

Posted: Fri Jun 10, 2005 4:13 am
by malcolmboston
quality, havent even read any books about it and you know that much 8O

i read your example post in the regex forum, nice stuff and it is excellent but for someone as nooby with regex as me it goes WAAAAAAY over my head, i think what would help to let people grasp the bare essentials and move onto the more advanced stuff some very simple examples of matching all the numbers in a string for eg, of a certain character in a paragraph etc, with a full explanation of what every symbol does in the call..

more work for you but more learning for me :wink:

Posted: Fri Jun 10, 2005 4:14 am
by phpScott
please do. I used(abused) a friend back in CA when it came to regex and he came from a perl background.

the heavy use of regex in perl is what has stopped me from become comforatable with it. :?

Posted: Fri Jun 10, 2005 4:19 am
by Syranide
a little tip from my own thumbnailer, don't find out the version, I just check if the functions exists, such as with gifsupport and so on not being supported in all builds, instead of checking a variable, I check if the function exists, same thing... but theoretically more stable ;)

Posted: Fri Jun 10, 2005 4:22 am
by malcolmboston
yeah, i thought of this idea too, cant believe no-one has mentioned it yet :lol: , anyway, yeah that is probably what i will end up doing, however i have a use for this in another thing that cannot be checked in the way you describe.

Plus its a good learning experience for us all :roll:

Posted: Fri Jun 10, 2005 4:31 am
by Syranide
uhm yeah, another warning for the previous regex-codes, they will likely not work it all gd-versions, as some versions apparently only report 2 numbers, such as 2.0 ... since you are only gonna use the first one (?) I would say just skipping the rest would be best, who knows what they come up with :S

(alternatively, copy from "(" to " " and use version_compare.

Posted: Fri Jun 10, 2005 4:35 am
by Chris Corbyn
Syranide wrote:uhm yeah, another warning for the previous regex-codes, they will likely not work it all gd-versions, as some versions apparently only report 2 numbers, such as 2.0 ... since you are only gonna use the first one (?) I would say just skipping the rest would be best, who knows what they come up with :S

(alternatively, copy from "(" to " " and use version_compare.

Code: Select all

$string = "bundled (2.0.28 compatible)";
preg_match('/\((\d+)\.(\d+)(?:\.(\d+))?[^\)]*\)/', $string, $matches);
print_r($matches);
Where there's a will there's a way ;)

EDIT | I'll put some questions together. I've done that before for others too.