Archives for October 2009...

(4 results found)

Remove Spaces in PHP Strings

Mon Oct 26 01:07:57 2009

I'm still very much at the beginning of learning PHP and while programming this blog, I came across a wonderful PHP function that can be used to remove/replace spaces (amongst other things) in strings. I know it will come in handy for someone. It's called str_replace(); The syntax is as follows:

str_replace(item to be replaced, item to replace with, string to commit this function);
<?php	
 
//creating a string held inside a variable.
$origString = "This is the original string.";
//display the string.
echo $origString;

//replace " " width "". In other words, replace spaces with no spaces.
$newString = str_replace (" ", "", $origString);
//display the new string.
echo $newString;

?>

Counting PHP Array Repetitions

Sat Oct 17 04:07:54 2009

The ability to check an array in php and see if the items it contains reoccur somewhere else inside the array is something that comes in handy once in a while. If for example, you have a list of dates and you wanted to count how many times the same date reoccurs in the array and display the results, this is what you would do.

<?php
 $arrayToCount = array("1981","1973","2010","1973","1981","1973");

 foreach( $arrayToCount as $val ) {
   if ( array_key_exists( $val, $arrayCounter) ) { 
      $arrayCounter[$val]++; 
   }
   else { 
      $arrayCounter[$val] = 1;
   }
 }
	
 while( $element = each( $arrayCounter ) ){
 echo $element[ 'key' ].' ('.$element[ 'value' ].')';
}
?>

As is, the results of this would look like this

1981 (2)
1973 (3)
2010 (1)

Here's what's happening. Obviously, we have an array containing dates that we want to count shown on line 1. We use a foreach loop to run the contents of the loop for however many dates are contained inside the array. An if/else statement is used on line 4 through 9, which uses the PHP function array_key_exists()(line 4.), which checks to see if $val (the date being checked) matches a value previously looped through. If this function returns true the value inside $arrayCounter gets pushed up by 1 or ++ each time (see line 5). If the array_key_exists() on line 4 returns false, or in other words, no match is found, then the value 1 gets added to a new place in the $arrayCounter array. We then use a while statement to display each of the dates and their count values stored inside the associative array $arrayCounter(lines 12 ~ 14)

And that's about it. If you are new to PHP, I would recommend drilling this kinds of routine into your head and you'll get your head around it before you know it.

The Scope of Things

Wed Oct 14 02:20:48 2009

Sometimes it can be a little difficult to get the specific "child" you want in ActionScript 3 by calling its name directly. Also, the use of getChildAt() and getChildByName("") doesn't work because they are outside the scope of the current class in question. Well, this is where you could try casting.

For example, say that I have three classes. One called Main.as, one called Second.as and one called Third.as, each containing a single MovieClip containing a Square, Circle and Triangle respectively. Main imports Second and and Third and so becomes their "parent". The result is a Square, Circle and Triangle on the stage.

Now say that I want to access Third from Second to change the scale or some other property of Third's triangle. Well, I would need to use the this.parent.getChildAt(position).getChildAt(position) or maybe this.parent.getChildByName("second).getChildByName("triangle"), that is, if I had given them names. However, this is where you would run into problems. Flex/Flash just doesn't know what you are talking about. Instead, by casting all instances and clips to "MovieClip" and holding them inside variables, you will ensure that Flex/Flash knows exactly what you want to access and what type of object it is.

Here's what I'm talking about

    //this.parent.getChildAt(1).getChildAt(0) just doesn't work. It's outside the scope
    //of the class called Third.

    var sec:MovieClip = MovieClip(this.parent.getChildAt(1));
    var tri:MovieClip = MovieClip(sec.getChildAt(0));
    tri.scaleX = 2; 

I know that some will complain that there is a better way to code and a good coder wouldn't code this way, and I would say I agree for the most part. However sometimes, whether it be a quick fix, or a quick add-on to an existing application coded by someone else, sometimes this will come in handy. After all, we can't guarantee the work of others.

If you would like a closer look, I have provided some fully annotated files here

FXG in ActionScript 3

Mon Oct 12 12:58:53 2009

With the introduction of the Flex 4 SDK (Gumbo), it is now possible to import FXG files into your ActionScript and MXML projects. In this example, I will be using an illustration of a flower, aptly named Flower.fxg which was created in and exported from Illustrator CS4.

I would also like to add that what I'm about to do is also possible using SVG files, and for anyone using TextMate or FlashDevelop, this looks a very promising replacement to the Flash IDE and library.

First, you need to create a package and embed your vector graphic as a Class. Embedding required the Flex SDK, so for Flash users who wish to try this out, you will need to get the Flex SDK and point flash in the right direction.

The finished article looks something like this:
package  {
      import flash.display.Sprite;
      import flash.events.MouseEvent;

      [SWF(width='500', height='500', backgroundColor='#FFFFFF', frameRate='60')]
	
      public class Main extends Sprite {
 	
	    [Embed(source='Flower.fxg')]
	    private var Flower:Class;
	    private var flower:Sprite = new Flower();
 
	    public function Main() {
	       addChild(flower).addEventListener(MouseEvent.CLICK, clickHandler);
	       flower.buttonMode = true;
	    }
		
	    private function clickHandler(event:MouseEvent):void {
	       trace("Flower Clicked");
            }
      }
}

After this class has been created, simply compile it making sure that the FXG file is where it's supposed to be. In my case, I used TextMate and the FLEX 4 SDK to compile. The result is as follows.

Get Adobe Flash player

Pretty cool stuff if you ask me. Anyway, for anyone interested, you can find the files for this here

Most recent posts

  1. Flashcards Maker - the app the helps you learn in a flash
  2. Convert Font Suitcase to TTF in 3 Easy Steps
  3. Rain or Shine - Weather Forecast Available Now!
  4. Getting around Apple's silly rules for iOS 10
  5. Localising App Names for iOS and Android

Search site:

Apps by Ubacoda.com

Click the app icons to see more info


Archives

Categories