Monthly Archives: July 2005

Generating JavaScript strings

Well, I’ve always experienced the generating JavaScript strings with PHP as a PITA. An example, which requires you to take care of the escaping of quotes, is the string: ‘O’Reilly has nice books’. Today i had this brilliant idea to do it as following:

<?php
$str = addslashes("Hello peter's cats");
echo "<script type='text/javascript'>";
echo "alert('$str')";
echo "";
?>

value-of in an attribute

There were days that i didn’t like XSL because it seemed to be impossible to insert a value inside an attribute. For example:

<form action="<xsl:value-of select="/page/action"/>" method="post">

Today i’ve seen that other people also struggle with this issue. So here is the solution:

<form action="{/page/action}" method="post">
</form>

Read from STDIN without echoing the input back

Today i was looking for a way to read passwords from a PHP-CLI script. So it was important the password didn’t appear on the console. I wrote a ttyecho function that uses stty to change the terminal line settings.

Scriptable browser

Last couple of days i’ve been trying out Simple Test. It allowed me stop stop echo and print_r variables all over the place. The package also has a Scriptable Browser.

At smscity.be you can earn credits each day. Therefor you have to visit their site and click some links. I wrote a smscity.txt script that does this for me.

Now all i had to do is make sure this script is executed each day, so i edited my crontab. It looks like:

###############################################################################
#
# Author: Tim Van Wassenhove
#
###############################################################################
# $Id:
###############################################################################

  @reboot                               /usr/bin/fetchmail -d 1800
  0,10,20,30,40,50      *  *  *  *      /usr/bin/wget -O /dev/null http://timvw/cron/blogmarks.php &gt; /dev/null 2&gt;&amp;1
  30                   02  *  *  *      /usr/bin/wget -O /dev/null http://timvw/cron/smscity.php &gt; /dev/null 2&gt;&amp;1

# *                     *  *  *  *      *
# |                     |  |  |  |      |
# |                     |  |  |  |      +----- command to be executed
# |                     |  |  |  +------------ day of week (1 - 7) (monday = 1)
# |                     |  |  +--------------- month (1 - 12)
# |                     |  +------------------ day of month (1 - 31)
# |                     +--------------------- hour (0 - 23)
# +------------------------------------------- min (0 - 59)

I am the manual

I found this in my inbox on my favorite PHP forum, DevNetwork

I am the manual

Hi timvw,
you've won an award!

I Am The Manual Award


And the beauty is, you now have a badge to show for it! Smile

You can download it and use them as your avatar or in your signature:

Congratulations
patrikG

_________________
Forum Rules
How to get what you want!
PHP Manual!
This isn't the code you're looking for.

GNU text utilities

More and more i seem to recieve requests from people that need to manipulate some text files.
And they don’t feel like doing it manually. So here are some examples of how i used the
GNU text utilities:

I want to split the file below into one with the questions and one with the answers. This can be easily done:

Who is the manual.*I am
Who am i.*The manual
What was HLN's first #1 hit.*Power of love
Which News man is the oldest.*Huey
About how many months did it take to record Small World.*4
Who wrote Jacob's Ladder.*Bruce Hornsby
cut -d "." -f1 trivia.txt > questions.txt
cut -d "*" -f2 trivia.txt > answers.txt

I want to know how many times people have logged in. With last i recieve output like below.
I generate a top10 list:

xabre    pts/0        cable-213-132-14 Wed Jul  6 14:41   still logged in
veurm    pts/16       80.236.195.247   Wed Jul  6 14:28   still logged in
timvw    pts/6        ip-213-49-86-220 Wed Jul  6 14:21   still logged in
ward     pts/1        dd5768f62.access Wed Jul  6 14:04   still logged in
bokkerij pts/1        91.42-201-80.ads Wed Jul  6 13:57 - 14:00  (00:02)
cowke    pts/54       215-157.241.81.a Wed Jul  6 13:37    gone - no logout
mette    pts/51       ip-213-49-112-10 Wed Jul  6 13:36   still logged in
esumlu   pts/49       176.228-201-80.a Wed Jul  6 13:25   still logged in
veurm    pts/6        ip-213-49-84-129 Wed Jul  6 13:21 - 14:10  (00:49)
trooperz pts/6        160.148-136-217. Wed Jul  6 13:05 - 13:21  (00:15)
gunther  pts/40       d5153d0d9.access Wed Jul  6 12:28   still logged in
timvw    pts/34       ip-213-49-86-220 Wed Jul  6 11:02 - 14:21  (03:19)
last | cut -d " " -f1 | sort | uniq -c | sort -rn | head

I want to use this file to generate two files, one for small galleries (<10 photos) and one for large
galleries (>= 10 photos)


http://example.com/gallery1|Fishing|18


http://example.com/gallery2|Fishing|5


http://example.com/gallery3|Fishing|6


http://example.com/gallery4|Fishing|5


http://example.com/gallery5|Fishing|35


http://example.com/gallery6|Fishing|2


http://example.com/gallery7|Fishing|1


http://example.com/gallery8|Fishing|9

grep  -E "(.*?)\|[0-9]{1}$" galleries.txt > small.txt
grep -vE "(.*?)\|[0-9]{1}$" galleries.txt >large.txt