Re: [PHP] array_search function bugged? update

2006-03-27 Thread Robin Vickery
On 27/03/06, je killen [EMAIL PROTECTED] wrote:
 Hi all
 Here is an update on my problem reported with the array_search function.
 to those who directed me to the bug report page, thanks.
 There are a number of reports specific to this function. Maybe event
 someone else has written a fix.
 here is my solution to my problem.

There are no open bug reports for array_search(). Loads of bogus ones though.

array_search() also completely the wrong tool for the job - the code
you posted is much more complicated and slower than it needs to be.

That whole palaver that you go through to produce $reduced could be
replaced with:

   $reduced = array_merge(array(), array_unique($str));

That nested loop you use to produce $final would be rather simpler and
faster if you just flipped $reduced and used a hash lookup.

   $charmap = array_flip($reduced);
   foreach ($str as $char) { $final[] = $charmap[$char]; }

-robin

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] array_search function bugged?

2006-03-21 Thread Richard Lynch
On Mon, March 20, 2006 8:35 pm, je killen wrote:

 I really don't understand, though, why you are doing things the way
 you describe...
 I only want one image file for each distinct letter, no repeats
 because
 I can't put two or more files with the same name
 in the same dir. The images need to be drug around DHTML style and
 reassembled into the correct word. Javascript
 needs to be able to establish when the chars are in the correct order
 so I need to have php feed it a formula to refer to.

Okay, now we are getting somewhere!

The easiest way to meet the unique filename requirement is to name
each file after its letter:
a.png
b.png
c.png

Of course, they only need to APPEAR that way to the browser and
Javascript.

In reality, you may not actually have a file named a.png at all.

For example, if your HTML is like this:
img src=/drawchar.php/a.png /

And you have a PHP script drawchar.php like this:
?php
  //EG: $path_info will be /a.png,
  //because that is what is in tha URL AFTER the PHP script name
  $path_info = $_SERVER['PATH_INFO'];

  //get JUST the one letter
  $char = substr($path_info, 1, 1);

  //Create the image:
  $image = imagecreatetruecolor(40, 10);

  //Boring colors, but works for now:
  $white = imagecolorallocate($image, 255, 255, 255);
  $black = imagecolorallocate($image, 0, 0, 0);

  //make the background white:
  imagefilledrectangle($image, 0, 0, 39, 9, $white);

  //draw the character:
  $font = 3; //Fonts can be 1 through 5
  $x = 2;
  $y = 38;
  imagechar($image, $font, $x, $y, $char, $black);

  //Find out how big the image is:
  ob_start();
  imagepng($image);
  $image_raw = ob_get_contents();
  ob_end_clean();
  $image_length = strlen($image_raw);

  //Send suitable headers:
  header(Content-type: image/png);
  header(Content-length: $image_length);

  //Send the data:
  echo $image_raw;
?

You can now just pass the word 'dissatisfaction' around, and use PHP
or JavaScript to output individual images.

I'll do it in PHP:

?php
  $word = 'dissatisfaction';
  for ($i = 0, $len = strlen($word); $i  $len; $i++){
$char = $word[$i]; //or use substr if you like
echo img src=\/drawchar.php/$char.png\ /;
  }
?

I haven't run this code to test it, so there might be a typo, but it's
going to be MUCH easier to maintain than what you've got going...

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] array_search function bugged?

2006-03-21 Thread Richard Lynch
On Mon, March 20, 2006 8:47 pm, je killen wrote:
 I am using simple for loops. If I create and populate an array and
 test
 it with a print statement
 in a for loop, it prints out all the current values. It I again call
 it
 to print in a for loop in subsequent code
 it has exhibited a degraded state.

Show us source code that does this.

You've made a mistake in your code.

Most likely you've altered the array without realizing it, or your
variables in your for() loops are mixed up.

Easy to do as a beginner.

 However, if your familiar with
 FreeBSD, it has a ports and packages
 mechanism for installing software. I had difficulty with it when
 installing Apache so I bypassed it and
 installed Apache from source just according to the install

There is simply no way this is your cause.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] array_search function bugged?

2006-03-21 Thread Jim Lucas

Richard Lynch wrote:

On Mon, March 20, 2006 8:35 pm, je killen wrote:

  

I really don't understand, though, why you are doing things the way
you describe...
  

I only want one image file for each distinct letter, no repeats
because
I can't put two or more files with the same name
in the same dir. The images need to be drug around DHTML style and
reassembled into the correct word. Javascript
needs to be able to establish when the chars are in the correct order
so I need to have php feed it a formula to refer to.



Okay, now we are getting somewhere!

The easiest way to meet the unique filename requirement is to name
each file after its letter:
a.png
b.png
c.png

Of course, they only need to APPEAR that way to the browser and
Javascript.

In reality, you may not actually have a file named a.png at all.

For example, if your HTML is like this:
img src=/drawchar.php/a.png /

And you have a PHP script drawchar.php like this:
?php
  //EG: $path_info will be /a.png,
  //because that is what is in tha URL AFTER the PHP script name
  $path_info = $_SERVER['PATH_INFO'];

  //get JUST the one letter
  $char = substr($path_info, 1, 1);

  //Create the image:
  $image = imagecreatetruecolor(40, 10);

  //Boring colors, but works for now:
  $white = imagecolorallocate($image, 255, 255, 255);
  $black = imagecolorallocate($image, 0, 0, 0);

  //make the background white:
  imagefilledrectangle($image, 0, 0, 39, 9, $white);

  //draw the character:
  $font = 3; //Fonts can be 1 through 5
  $x = 2;
  $y = 38;
  imagechar($image, $font, $x, $y, $char, $black);

  //Find out how big the image is:
  ob_start();
  
At this point, could you have an switch statement that checked the 
extension that you sent in the url and make it run the corresponding 
imagepng(), imagejpg(), and imagegif() ?

  imagepng($image);
  $image_raw = ob_get_contents();
  ob_end_clean();
  $image_length = strlen($image_raw);

  //Send suitable headers:
  

and then here send the corresponding header too.

Just a thought about making it even more universal.

jl

  header(Content-type: image/png);
  header(Content-length: $image_length);

  //Send the data:
  echo $image_raw;
?

You can now just pass the word 'dissatisfaction' around, and use PHP
or JavaScript to output individual images.

I'll do it in PHP:

?php
  $word = 'dissatisfaction';
  for ($i = 0, $len = strlen($word); $i  $len; $i++){
$char = $word[$i]; //or use substr if you like
echo img src=\/drawchar.php/$char.png\ /;
  }
?

I haven't run this code to test it, so there might be a typo, but it's
going to be MUCH easier to maintain than what you've got going...

  


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] array_search function bugged?

2006-03-20 Thread Richard Lynch
On Sun, March 19, 2006 4:46 pm, je killen wrote:
 The following code does not produce the correct results (for my
 purposes):

 function code($str, $match, $formula)
   {
 for($i = 0; $i  count($str); $i++)
   {
 $formula[$i] = array_search($str[$i], $str);// ==|| no bueno
//print $formula[$i]; not right
   if($formula[$i]  $i)
{$str[$i] = '';}
}
   //print'br'; //- etc (lots more code)---

 The code takes a string of ascii letters forming a word and is
 supposed
 to create a list of indexes
 in the proper sequence for reconstructing the word. The object of the
 code in context is to take
 any word and create an array of unique letters with no repeats so that
 gd can be used to produce
 images of each letter.

Almost for sure, this function could be used instead:
http://www.php.net/manual/en/function.count-chars.php

You'll be throwing away the information about how many of each letter.

I really don't understand, though, why you are doing things the way
you describe...

 The letters are then reassembled in the browser
 to form the word. The
 $formula above is supposed to tell the browser in what sequence to
 display each letter, including
 using the same letter image in repeat locations. For this it fails
 miserably.

 These are the results of test steps:

 dissatisfaction (the test word)
 Processed input string:   dissatisfaction (code output at key step to
 verify)
 at creation of formula:012245128410511314 (formula sampled at ' no
 bueno' line in code)
 from browser source array:012245128410511314 (formula pasted from
 browser javascript source)

 The letter images spell out:
 disstfisntidfiiait  which is in accordance with the formula as far as
 I
 can tell.

How do you distinguish in:
012245128410511314
between one-digit and two-digit integers?

dissatisfaction
  1
012345678901234

0-d
1-i
2-s
3-NULL (2 is 's')
4-a
5-t
6-NULL (1 is 'i')
7-NULL (2 is 's')
8-f
9-NULL (4 is 'a')
10-c
11-NULL (5 is 't')
12-NULL (1 is 'i')
13-o
14-n

Going from just the numbers, now I get:
012245128410511314
dissatisfaidtiiNULLia

So, given the input 012245128410511314, you are never going to get the
original word back from just that...

I don't think your formula makes sense in the first place...

If you just want to represent each character as a GD image, and not
duplicate, so the browser can cache images, you can just do:

?php
for ($i = 0, $len = strlen($word); $i  $len; $i++){
  $char = $string[$i];
  ?img src=drawchar.php/char=?php echo $char? /?php
}
?

You can then use $_SYSTEM['PATH_INFO'] (or whatever it's called) to
get 'char=?' and do like:
list($key, $value) = explode('=', $_SYSTEM['PATHINFO']);
$$key = $value;

 There is one other weakness I've discovered, if arrays are created in
 one code sequence and are called
 to print in loops more than once or processed in different code
 sections. The second time the same array
 is called it has significantly degraded, loosing values from index
 positions.

If you are using list/each or something like that, there is an
internal pointer in the array which you are mucking with, and most
likely confusing yourself with.

Try using 'foreach' which does not use that internal pointer.

 the seems to be no bug report accommodation in the php.net site so I'm
 posting here.

Posting here FIRST is good, especially since you have not encountered
an actual bug yet :-)

After that, though, there *IS* a place to search for, and ultimately
report bugs:
http://bugs.php.net

But please don't clog that with reports before checking them out
thoroughly, so you really ARE reporting bugs in PHP.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] array_search function bugged?

2006-03-19 Thread Richard Davey

On 19 Mar 2006, at 22:46, je killen wrote:

The code takes a string of ascii letters forming a word and is  
supposed to create a list of indexes
in the proper sequence for reconstructing the word. The object of  
the code in context is to take


Ignoring your code (and the supposed 'bug') for a second - why don't  
you just use the count_chars() function? After all, it is designed to  
do almost exactly what you are trying to recreate here.


http://uk2.php.net/manual/en/function.count-chars.php

the seems to be no bug report accommodation in the php.net site so  
I'm posting here.


I'm not sure you looked very hard. There is a link that says  
'reporting bugs' in the top nav, which takes you here: http:// 
bugs.php.net/


Cheers,

Rich
--
http://www.corephp.co.uk
Zend Certified Engineer
PHP Development Services

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php