PHP Cookbook: Parsing Comma-Separated Data

Problem
You have data in comma-separated values (CSV) format—for example, a file exported from Excel or a database—and you want to extract the records and fields into a format you can manipulate in PHP.

Solution
If the CSV data is in a file (or available via a URL), open the file with fopen( ) and read in the data with fgetcsv( ) . Example 1-31 prints out CSV data in an HTML table. (more…)

Continue ReadingPHP Cookbook: Parsing Comma-Separated Data

PHP Cookbook: Controlling Case

Problem
You need to capitalize, lowercase, or otherwise modify the case of letters in a string. For example, you want to capitalize the initial letters of names but lowercase the rest.

Solution
Use ucfirst( ) or ucwords( ) to capitalize the first letter of one or more words, as shown in Example 1-25.

Example 1-25. Capitalizing letters
[sourcecode language=”php”]
<?php
print ucfirst("how do you do today?");
print ucwords("the prince of wales");
?>
[/sourcecode]
(more…)

Continue ReadingPHP Cookbook: Controlling Case

PHP Cookbook: Expanding and Compressing Tabs

Problem
You want to change spaces to tabs (or tabs to spaces) in a string while keeping text aligned with tab stops. For example, you want to display formatted text to users in a standardized way.

Solution
Use str_replace( ) to switch spaces to tabs or tabs to spaces, as shown in Example
1-22.
Example 1-22. Switching tabs and spaces (more…)

Continue ReadingPHP Cookbook: Expanding and Compressing Tabs

Belajar PHP: Mendapatkan Data Siswa Berdasarkan Nomor NISN Secara Realtime

Dalam tutorial kali ini kita akan mencoba mendapatkan data pokok dari seorang siswa yang telah memiliki NISN (Nomor Induk Siswa Nasional) yang dikelola oleh Dapondik, Kemendikbud. Tutorial ini akan bertumpu pada penggunaan library cURL dan manipulasi array-string sederhana.

Prinsip Kerja

Pada dasarnya, data pokok yang akan kita ambil berasal dari hasil pencarian pada situs Dapondik Kemendikbud (http://nisn.dapondik.org/). URL yang dituju adalah http://nisn.dapondik.org/siswa.php. Dengan library cURL, kita akan posting variabel $_GET berupa nisn dengan value nomor NISN seorang siswa. Hasil eksekusi cURL tersebut akan kita olah dengan teknik parsing dan replacing data.

Source Code

Berikut adalah source code dari fungsi untuk mengambil data pokok Siswa berdasarkan NISN dengan PHP: (more…)

Continue ReadingBelajar PHP: Mendapatkan Data Siswa Berdasarkan Nomor NISN Secara Realtime

PHP Cookbook: Extracting Substrings

Problem
You want to extract part of a string, starting at a particular place in the string. For example, you want the first eight characters of a username entered into a form.

Solution
Use substr( ) to select your substring, as in Example 1-9. Example 1-9. Extracting a substring with substr( )
[sourcecode language=”php”]
<?php
$substring = substr($string,$start,$length);
$username = substr($_GET[‘username’],0,8);
?>
[/sourcecode]

Discussion (more…)

Continue ReadingPHP Cookbook: Extracting Substrings