opening file then reading into variable

[problem]

You want to open save the contents of a file, into a variable in PHP.

[/problem]

[solution]

Simple demo on opening and reading contents of files in PHP.

Script under example tab takes filename as a parameter, then opens it read-only and reads contents in data variable.

Then it echoes a header and footer line, with the data in the middle.

[/solution]

[example]

<?php

$filename=$argv[1];
$FH=fopen("$filename","r") or die("could not open $filenamen");

while(!feof($FH)) { $data.=fgets($FH); }

fclose($FH);

echo "###### beginning of $filename ######n";
echo "$data";
echo "n###### end of $filename ######n";

exit(0);

?>

Here is a run through:


$ php -q readfiles.php rhyme.txt
###### beginning of rhyme.txt ######
Mary had a little lamb,
It was always bleating.
###### end of rhyme.txt ######

[/example]

[reference]

[tags]PHP file handling, PHP Coding School[/tags]

[/reference]

how to interperse HTML with PHP

[problem]

You want to intersperse HTML with PHP code.

[/problem]

[solution]

Just getting back to basics, this is a very simple demo
of how to implement PHP code along with HTML.

[/solution]

[example]

<html><head>....
<body>
<h1>Hello World</h1>
<?php if($_GET[runit]) { ?>
<b>This will get displayed if ?runit=yes is
appended to the web users request</b>

<?php } else { ?>

nothing to see here ... :)

<?php } ?>

<body><html>

[/example]

[reference]

[tags]Interperse HTML in PHP, PHP Coding School[/tags]

[/reference]

working with arrays part two

[problem]

You want to create an array, then traverse it.

[/problem]

[solution]

Following up from part one – working with arrays.

To spin through all elements in our names array, here is the code below.

[/solution]

[example]

<?php

$count=count($names);
for ($i=0;$i<$count;$i++) { print($names[$i]."n"); }

?>

Then for our string index array (hash):

<?php

while(list($k,$v)=each($addresses)) {
echo "$k lives at $vn";
}

?>

[/example]

[reference]

[tags]PHP Arrays, PHP Hashes, PHP Coding School[/tags]

[/reference]

working with arrays part one

[problem]

You want to work with string index arrays. Sometimes called Hashes.

[/problem]

[solution]

PHP can handle number or string indexed arrays.

Arrays can be initialized in a number of ways. For example:

Basic array assignment and initialization, without specify numbers or strings:

<?php

$names[]="joe";
$names[]="john";
$names[]="jim";

?>

[/solution]

[example]

Specifically assigning values to numbered elements in the names array:

<?php

$names[0]="joe";
$names[1]="john";
$names[2]="jim";

?>

Quicker way to assign values to names array:

$names=array(“joe”,”john”,”jim”);

Assigning values to a string indexed array – or hash (key and value pairs):

<?php

$addresses=array(
"joe" => "1 Marble Street",
"john" => "12 Marble Street",
"jim" => "23 Marble Street"
);

?>

[/example]

[reference]

[tags]PHP, Arrays, PHP Coding School[/tags]

[/reference]

regular expression with ereg and eregi

[problem]

You want to perform regular expression with PHP.

[/problem]

[solution]

Again PHP implementation of regular expression is very similar to Perl.

In the simpliest form, pattern matching:

$ php -q<<EOT
<?php

$str="hello world";
if(ereg("world","$str")) {
echo("got a match");
}

?>
EOT

got a match

[/solution]

[example]

Similarly eregi operates the same, just it is case insensitive.

$ php -q<<EOT
<?php

$str=”hello world”;
if(eregi(“WORLD”,”$str”)) {
echo(“got a matchn”);
}

?>
EOT

got a match

[/example]

[reference]

[tags]PHP ereg, PHP eregi, PHP Coding School[/tags]

[/reference]

string manipulation strlen and chr

[problem]

You want to perform string manipulation with PHP.

[/problem]

[solution]

Following on from the intro post, see the example tab for some more demos on PHP string manipulation.

[/solution]

[example]

Counting the number of characters in a string:

strlen

$ php -q<<EOT
<?php

echo(strlen("Hello World")."n");

?>
EOT

11

Converts an ASCII code to a character:

chr

$ php -q<<EOT
<?php

echo(chr(65)."n");

?>
EOT

A

[/example]

[reference]

[tags]PHP strlen, PHP chr, PHP Coding School[/tags]

[/reference]

string manipulation substr and trim

[problem]

Substituting and trimming string variables in PHP.

[/problem]

[solution]

Similar to Perl, PHP has an extensive library of functions.

See the example tab for a few simple examples, showing the basic syntax.

[/solution]

[example]

substr

$ php -q<<EOT
<?php

echo(substr("Hello World",0,4)."n");

?>
EOT

Hell

trim

 $ php -q<<EOT
<?php

echo(trim(" Hello World ")."n");

?>
EOT

Hello World

[/example]

[reference]

[tags]PHP substr, PHP trim, PHP Coding School[/tags]

[/reference]

epoch generation and converting

[problem]

You want to generate the current epoch offset for UNIX. The count forward since 8am on 1st Jan 1970.

Or conversely you want to see the date and time, for a given epoch offset.

[/problem]

[solution]

You can use perl’s mktime or localtime – or just use my tools below.

[/solution]

[example]

Either enter epoch:

Or enter the date (plus time)

day month year

— select day —
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

— select month —
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec

— select year —
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049

hr mins secs

— select hour —
00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23

— select mins —
00
01
02
03
04
05
06
07
08
09
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

— select secs —
00
01
02
03
04
05
06
07
08
09
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

epoch offset or date will appear here

[/example]

[reference]

[tags]epoch, unix, 2038[/tags]

  1. techno park epoch year 2038 problem

[/reference]

Vertical label creation

[problem]

You want to generate a vertical image, with text over the top.

[/problem]

[solution]

Here is some code I wrote, which is used extensively on my sites:

  

[/solution]

[reference]

Give it a blast on this site, like this: http://coding-school.com/common/label-up.php?label=your+text+here

Automatically calculates size required. You can also add font=# – where # is 1 to 5, like this:

http://coding-school.com/common/label-up.php?label=your+text+here&font=3

I’ve now created an online tool, using this code

[/reference]

[tags]Image, PHP[/tags]