Kategori
PHP

String Functions in the PHP

Discussion Board: Introduction to the various functions – the basic string functions (which are often used) in PHP and its possible use.
Kebutuhan : Webserver Package, already installed.

Text is one of the sources of unstructured data is greatest, because most of the information stored in the text, whether text in a variety of filetype (doc txt pdf etc) and web-text (html). Today more often performed research involving the text, and computer science that involves the text of which is the Natural Language Processing, Text (Web) Mining, or Information Retrieval.

Here are some of functions of a text (string) in PHP which will often be used if you are struggling with studies involving text processing.

1. Adding a slash (\) in the string -> addslashes.
[sourcecode language=”php”]
<?php
$text = "This is Anne’s book.";
echo addslashes($text);
?>
[/sourcecode]
Output :
This is Anne\’s book.

Why is this important? In text processing, it is often a sign of the quote (‘) will cause an error when done parsing of the text, especially when we use regular expressions.

2. Breaking the string with the string -> explode.
[sourcecode language=”php”]
<?php
$sentence = "My name is June";
$word = explode(" ", $sentence);
print_r($word);
?>
[/sourcecode]
Output :
Array
(
[0] => My
[1] => name
[2] => is
[3] => June
)

Explode will break up the text and divide it into an array, like the example above, $text in-explode with the string “” (space). This is the basis of tokenizing algorithm, namely breaking the sentence in the word – the word constituent. Tokenizing will often be used in text processing.

3. Combining an array of strings in one string -> implode.
[sourcecode language=”php”]
<?php
$arrayWord = array(‘This’, ‘is’, ‘a’, ‘sentence’);
$joinString = implode(" ", $arrayWord);
echo $joinString;
?>
[/sourcecode]
Output :
This is a sentence

Implode, as seen from the example above (example: arrays in the sample-implode / merged by using the string space), is the opposite of the function No. 2 (explode). Implode function has an alias, which is Join.

4. Provides MD5 value from string -> md5.
[sourcecode language=”php”]
<?php
$string = ‘password’;
$stringMd5 = md5($string);
echo $stringMd5;
?>
[/sourcecode]
Output :
5f4dcc3b5aa765d61d8327deb882cf99

MD5 is a hash function (cryptographic one-way) is quite well known and often used for security (although it’s a lot md5 decryptor). Md5 function like this will often be used when we build an application that requires authentication (eg to log into the system).

5. Removes HTML tags in the string -> strip_tags
[sourcecode language=”php”]
<?php
$text = ‘<b>June Susan</b> <i> June June</i>’;
echo strip_tags($text); echo ‘<br>’;
echo strip_tags($text, ‘<i>’); // Allow tag <i>
?>
[/sourcecode]
Output :
June Susan June June

Before we can process the text from the web (which is usually in the form of hypertext / html), then we must be “clean” tag – html tags that exist in the text. This is where the strip_tags function is useful. We can remove all tags, or allow certain tags to remain in the text (see examples).

6. Calculating the length of string -> strlen.
[sourcecode language=”php”]
<?php
$string = ‘I am a superman’;
echo strlen($string);
?>
[/sourcecode]
Output :
15

Strlen count the number of characters (length) of the string, spaces, and symbols – symbols that exist in the string will also be calculated in function strlen.

Happy coding, then! 😀

Kategori
PHP

Fungsi – Fungsi String dalam PHP

Discussion Board : Pengenalan berbagai fungsi – fungsi String dasar (yang sering digunakan) dalam PHP beserta contoh penggunaannya.
Kebutuhan : Webserver Package, already installed.

Text adalah salah satu sumber unstructured data yang paling besar, karena sebagian besar informasi tersimpan dalam text, baik text dalam berbagai filetype (doc txt pdf dll) maupun web text (html). Dewasa ini semakin sering dilakukan riset yang melibatkan text, dan bidang ilmu komputer yang melibatkan text diantaranya adalah Natural Language Processing, Text (Web) Mining, ataupun Information Retrieval.

Berikut adalah beberapa fungsi text (string) dalam PHP yang akan sering digunakan apabila anda berkutat dengan penelitian yang melibatkan text processing.

1. Menambahkan slash (\) dalam string –> addslashes.
[sourcecode language=”php”]
<?php
$text = "This is Anne’s book.";
echo addslashes($text);
?>
[/sourcecode]
Output :
This is Anne\’s book.

Kenapa hal ini penting? Dalam pemrosesan teks, seringkali tanda quote (‘) akan menimbulkan eror ketika dilakukan parsing terhadap text tersebut, terutama ketika kita menggunakan regular expression.

2. Memecah string dengan string –> explode.
[sourcecode language=”php”]
<?php
$sentence = "My name is June";
$word = explode(" ", $sentence);
print_r($word);
?>
[/sourcecode]
Output :
Array
(
[0] => My
[1] => name
[2] => is
[3] => June
)

Explode akan memecah text dan membaginya ke dalam array, seperti contoh diatas, $text di-explode dengan string ” ” (spasi). Hal ini merupakan dasar dari algoritma tokenizing, yaitu memecah kalimat dalam kata – kata penyusunnya. Tokenizing akan sering sekali digunakan dalam text processing.

3. Menggabungkan array string dalam satu string –> implode.
[sourcecode language=”php”]
<?php
$arrayWord = array(‘This’, ‘is’, ‘a’, ‘sentence’);
$joinString = implode(" ", $arrayWord);
echo $joinString;
?>
[/sourcecode]
Output :
This is a sentence

Implode, seperti terlihat dari contoh diatas (contoh : array dalam contoh di-implode / digabung dengan menggunakan string spasi), merupakan kebalikan dari fungsi no 2 (explode). Fungsi implode memiliki alias, yaitu Join.

4. Memberikan nilai MD5 dari string –> md5.
[sourcecode language=”php”]
<?php
$string = ‘password’;
$stringMd5 = md5($string);
echo $stringMd5;
?>
[/sourcecode]
Output :
5f4dcc3b5aa765d61d8327deb882cf99

MD5 merupakan salah satu fungsi hash (kriptografi satu arah) yang cukup terkenal dan sering digunakan untuk security (meskipun sekarang sudah banyak md5 decryptor). Fungsi md5 seperti ini akan sering digunakan apabila kita membangun sebuah aplikasi yang memerlukan autentikasi (misal untuk login ke dalam sistem).

5. Menghilangkan HTML Tag dalam string –> strip_tags
[sourcecode language=”php”]
<?php
$text = ‘<b>June Susan</b> <i> June June</i>’;
echo strip_tags($text); echo ‘<br>’;
echo strip_tags($text, ‘<i>’); // Allow tag <i>
?>
[/sourcecode]
Output :
June Susan June June

Sebelum kita bisa memproses text dari web (yang biasanya berbentuk hypertext / html), maka kita harus “membersihkan” tag – tag html yang ada dalam text tersebut. Disinilah fungsi strip_tags berguna. Kita bisa menghilangkan seluruh tag, ataupun mengijinkan tag tertentu untuk tetap berada dalam text (lihat contoh).

6. Menghitung panjang string –> strlen.
[sourcecode language=”php”]
<?php
$string = ‘I am a superman’;
echo strlen($string);
?>
[/sourcecode]
Output :
15

Strlen menghitung jumlah karakter (panjang) dari string, spasi, dan simbol – simbol yang ada dalam string juga akan dihitung dalam fungsi strlen.

Happy coding, then! 😀