MYSQL String functions makes life easier

MYSQL strings function can make your life easier for you.You can use combination of string function to get the complex result needed in some project.String function in mysql helps us to extract what we want from a string and that can avoid lot of code.The common String functions available in MYSQL can be found on the link http://dev.mysql.com/doc/refman/5.1/en/string-functions.html

Use of MYSQL String function.

consider the following table which contains caseId and you have to extract maximum number at the last of caseId, those belong to id 1.

caseId id Value
JET/2005-2006/1 1 15
JET/2005-2006/2 1 25
JET/2005-2006/1 2 15
JET/2005-2006/2 1 2
JET/2005-2006/2 2 12
JET/2005-2006/3 1 5

you can use the following query to get the result.

SELECT max(substring(caseId,locate(”/”,caseId,locate(”/”,caseId)+1)+1)) as maxCaseId FROM `test` WHERE id=1

The result of the query is as follows.

maxCaseId
3

This query work as follows.

inner locate function get’s first occurrence of “/” [let's 4].and outer locate function get first occurrence of “/” [let's 17] in the string starting from position 4+1=5.so finally in subString function will give all the string values after position 17+1=18.So by this we will have all numbers those belongs to id 1.now finally max function will get max number within all the numbers found by substring function.

if you want to get the maximum integral value, you got to cast it to an integer - otherwise, on having a caseId value like JET/2005-2006/23, it would still return 3 as maximum & not 23.

SELECT
MAX(0 + SUBSTRING(caseId, LOCATE(’/', caseId, LOCATE(’/', caseId) + 1) + 1)) AS maxCaseId
FROM `test` WHERE id = 1;

SELECT
MAX(0 + RIGHT(caseId, LOCATE(”/”, REVERSE(caseId)) - 1)) AS maxCaseId
FROM `test` WHERE id = 1;

even reversing strings can avoid looping of locate.

SELECT MAX(RIGHT(caseId, LOCATE(”/”, REVERSE(caseId)) - 1)) AS maxCaseId FROM `test` WHERE id = 1

So This is just an example to explain uses of string functions available in MySql. There may be lot’s of other situation where you can use string function provided by mysql and get your work done rather than getting result by writing complex logic in programming.

acessing xml in php sent by other script

There are several situation where we need to accept xml as input in php file which is sent by other scripts like flash/php/perl etc.

To accept xml as input in php, first you need to check that content-type which is being passed by source script has to be in "text/xml" format.

Now in your PHP script you can accept xml input by the following code.

$xml_str=file_get_contents("php://input");
//where $xml_str will have xml string passed by the source script.


Mengambil Pola Teks

Sering sekali kita berhubungan dengan input atau data yang berhubungan dengan teks dan berkepentingan untuk mengambil sebagian dari teks tersebut berdasarkan suatu pola. Bagaimana kita melakukan hal tersebut ? Salah satu cara paling efisien adalah dengan menggunakan fungsi preg_match() seperti yang akan saya jelaskan di bawah ini.

Fungsi preg_match()

Fungsi preg_match() adalah fungsi yang digunakkan untuk mencari suatu pola di dalam subjek teks ktia dengan menggunakan regular expression.

Sintaks dasarnya adalah sebagai berikut:

int preg_match ( string pattern, string subject [, array &matches [, int flags [, int offset]]] )

tapi untuk penyederhanaan maka sintaks berikut adalah yang akan kita gunakan :

int preg_match ( string pattern, string subject [, array &matches] )

Dimana :
  • pattern adalah definisi pola dalam regex yang ingin kita cocokkan dengan subjek kita
  • subject adalah subjek teks yang akan kita cocokkan
  • matches adalah array tempat menyimpan hasil pencocokan teks dengan pola regex yang telah kita definisikan. Ini adalah parameter optional (boleh ada atau tidak).
Contoh Kasus

Misalkan kita memiliki suatu urutan teks sebagai berikut :

Nama=PHP;Website=http://www.komputasiawan.com;Komentar=Belajar Regex itu sangat berguna, terutama untuk pengolahan teks;Topik=Pengolahan Teks;

Dari untaian teks tersebut saya ingin mengambil url website yang terdapat di dalamnya, yaitu http://www.komputasiawan.com. Bagaimana caranya ?

Pertama kita tentukan pola regex yang pas untuk mengambil nilai dari entri Website, yaitu :

Website=(.+?);

Setelah itu kita masukkan sebagai parameter pertama fungsi preg_match() dengan diapit beberapa karakter khusus sebagai pengapit pola regex (/,@, &, dsbnya).

Contoh code-nya adalah sebagai berikut :


<?php
$teks = "Nama=PHP;Website=http://www.komputasiawan.com;Komentar=Belajar Regex itu sangat berguna, terutama untuk pengolahan teks;Topik=Pengolahan Teks;";

$pola = "&Website=(.+?);&";

$hasil_arr = array();
$hasil_akhir = "";

preg_match($pola, $teks, $hasil_arr);
$hasil_akhir = $hasil_arr[1];

echo "<pre>";
print_r($hasil_arr);

echo "URL website = $hasil_akhir";
echo "</pre>";
?>


Dan berikut adalah hasil yang didapatkan :


Array
(
[0] => Website=http://www.komputasiawan.com;
[1] => http://www.komputasiawan.com
)
URL website = http://www.komputasiawan.com


Sederhana bukan ?

Penutup

Penggunaan pola regular expression (regex) di PHP dengan menggunakan fungsi preg_match() sangat berguna untuk mengolah teks sederhana sampai dengan yang kompleks. Hal yang perlu kita kuasai adalah pengetahuan mengenai regex itu sendiri.

Demikian artikel ini dibuat, semoga dapat berguna. Nantikan artikel selanjutnya.

open files in a directory with dir() in php

<?php
//Open send directory
if(@$dir = @dir("send"))
{
//List files in send directory
while (($file = $dir->read()) !== false)
{
echo "filename: " . $file . "<br />";
}
$dir->close();
}
else
{
echo 'directory not opened';
}
?>

output :
 1)
directory not opened

2)
filename: .
filename: ..
filename: sendsms.html
filename: sendsms.php
filename: send_vard.php
filename: send_vcard_sms.html
filename: smser.php

note :
"send" is a directory name