Archiv der Kategorie ‘hacks‘

 
 

Cap Sentence

It’s odd sometimes the code that comes one when your tired:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function sentenceCap($str){
    $alphanumeric = "/[a-zA-Z0-9]/";
    $period = "/\./";
    $count = strlen($str);
    $newString = $str[0];
    for($i = 1; $i < $count; $i++){
      $curr = $str[$i];
      if (preg_match($period, $curr)){
        $nextCap  = true;
      }
      if(preg_match($alphanumeric, $curr)){
        if (!$nextCap){
          $curr = strtolower($curr);
        }else{
          $nextCap = false; 
        }
      }
      $newString .= $curr;
    } 
    return $newString;
  }

5 minutes on php.net… no luck… skrew it… 5 minutes of coding this absurd function. At least I can replace the implementation in the morning when I think of a better way to do it.

EDIT:
I didn’t have time to clean up the function yet but Evan Frohlich posted this really nice solution:

1
2
3
4
5
6
7
8
9
function sentenceCap($str){
    return preg_replace('/([.!?])\s*(\w)/e', "strtoupper('\\1 \\2')", ucfirst(strtolower($str)));
}
 
//Also regarding the above I think you should shift all of your strings 
//to Upper Case first. Otherwise un-capitalized words that begin a sentence will not be corrected.
 
//Line 4 should be:
$curr = strtoupper($str[$i]);

Digg

zQuery AS Weirdness

I was on the train the other day with my laptop and I though – hey, it would be pretty easy to write a hacky piece of code that will add method chaining to any AS object – and it would also be interesting if you could get at composed objects like the graphics class via a method call so that you could function chain those as well – obviously inspired by jQuery… but not really much like it… and significantly less useful (warning this is just an experimental hack):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
 
// slow
var t = getTimer();
 
// untyped to stress js style
var rect = $(new Sprite)
           .add("click", function(e){ e.target.rotation+=10 })
           .appendTo(this)             
           .rotation(45).x(100).y(100)
           .graphics()
           .beginFill(0)
           .drawRect(-50,-50,100,100);
 
var line = $(new Sprite)
           .prependTo(this)
           .attr({x:30, y:30, rotation : 10})
           .graphics()
           .lineStyle(0, 0xFF0000)
           .lineTo(100,100)
trace(getTimer() - t);
 
t = getTimer();
 
// fast
var line2;
with(line2 = addChild(new Sprite)){
	with(graphics) lineStyle(0,0x0000FF),
	lineTo(100,100);
	rotation = 10, x = 200, y = 30;
}
 
var rect2;
with(rect2 = addChild(new Sprite)){
	with(graphics) beginFill(0xFF0000), 
	drawRect(-50,-50,100,100);
	rotation = 45, x = 300, y = 100;
}
trace(getTimer() - t);
 
// zQuery joke function
function $(object:*):Object{ 
  var i:XML, access:String, nm:String;
  var wrapper:Object = {};
  var structure:XML = describeType(object);
  for each(i in structure.method){
	  nm = i.@name;
	  (function(nm):void{
	    wrapper[nm] = function():Object{
		  object[nm].apply(object, arguments);
		  return wrapper;
	    }
	   })(nm);
  }
  for each(i in structure.accessor){
	 access = i.@access;
	 nm = i.@name;
	 if (access == "readonly"){
		 (function(nm):void{
		     var o = $(object[nm]);
		     wrapper[nm] = function():*{
		       return o
			 }
		 })(nm);
	 }else if (access == "readwrite"){
		 (function(nm):void{
			 wrapper[nm] = function(value:*):Object{
				 object[nm] = value;
				 return wrapper;
			 }
		 })(nm);
	 }
  }
  wrapper.add = wrapper.addEventListener;
  wrapper.remove = wrapper.removeEventListener;
  wrapper.appendTo = function(target):Object{
	  target.addChild(object);
	  return wrapper;
  }
  wrapper.prependTo = function(target):Object{
	  target.addChildAt(object, 0);
	  return wrapper;
  }
  wrapper.attr = function(props):Object{
	  for (var i:String in props){
		 object[i] = props[i];
	  }
	  return wrapper;
  }
  return wrapper;
}

A few things worth noting. I did a little speed test against with statements which is pretty funny, the zQuery version takes 9ms and the with statements take less than 1ms. It might not be obvious, but the rect and the line vars are set to reference graphics objects not the sprites – there are a bunch of different ways to handle that…. but I’d never use this anywhere for real so I may not bother fixing it up. The attr() function is actually a useful utility to have hanging around – I’ve mentioned variations on it on actionsnippet many times.

A long time ago I saw this somewhere as3Query I’ve messed with it a bit – sort of tries to bring jQuery style syntax to AS. Worth checking out, not sure I’d use it on a real AS project though…

Digg

Great CDATA Trick – (Multiple Line String Literal)

One thing I like about php strings is that you can easily write them on multiple lines:

1
2
3
4
5
6
$theString = "
<!DOCTYPE html>
  <html>
    <head>
      <meta charset='utf-8' /> 
etc....";

It always bugged me that you couldn’t do this in actionscript. Turns out you can – I did some googling the other day in an attempt to find a way – and to my surprise I found this question on stackoverflow note the answer by Nick Sonneveld.

Here’s how it works:

txt.condenseWhite = true;
txt.htmlText = (<![CDATA
Some list elements one of the many poorly implemented html elements in flash...
  <li>one</li>
  <li>one</li>
  <li>one</li>
...nevertheless they can be highly useful with some hacky stylsheet stuff...
]]>).toString();

It’s usually the case that you want to use condenseWhite to remove extra whitespace – however being able to preserve returns and extra spacing may also be desirable.

I used this because I was manually copy and pasting information from an html version of a word doc – clicking a button in an air app I wrote to remove SOME of the html elements and attributes like and class=”MSsomething” and then pasting the final result into some weird timeline based flash templates (that someone else created). This little snippet saved me some serious time. The air app also did a lot of the work with some regular expressions. Converting things like:

<p>.           stuff</p>

to…

<li>stuff</li>

A funny side note: My syntax highlighter did NOT dig this AS syntax, so to get it to show up in a readable way, I wrote some php directly in the post (I have Exec-PHP installed);

1
2
3
4
5
6
7
8
9
10
11
<?php
echo htmlentities("txt.condenseWhite = true;
txt.htmlText = (<![CDATA
Some list elements one of the many poorly implemented html elements in flash...
  <li>one</li>
  <li>one</li>
  <li>one</li>
...nevertheless they can be highly useful with some hacky stylsheet stuff...
]]>).toString();
");
?>

Digg