Anda di halaman 1dari 6

Manipulasi string atau

kalimat di PHP
Posted on August 14, 2015 by Ambar Hasbiyatmoko

Di PHP, ada beberapa macam fungsi yang bisa kita gunakan untuk memanipulasi string atau
kalimat di PHP, yaitu :

1) STR_REPLACE()
Fungsi ini digunakan untuk mengganti huruf/kata dari suatu kalimat. Fungsi ini bersifat case
sensitive.
a) Mengganti kata :
?

1
2
3
4
5
6
7

<?php
$text = "Hello world";
echo str_replace("world", "you", $text); // Hello you
$text1 = "Hello world";
echo str_replace("World", "you", $text1); // Hello world
?>

b) Mengganti beberapa huruf sekaligus secara bersamaan :


?

1
2
3
4
5
6

<?php
$text = "Hello world";
$cari_huruf = array('H', 'r', 'd');
$huruf_baru = array('S', 'e', 'u');
echo str_replace($cari_huruf, $huruf_baru, $text); // Sello woelu
?>

c) Menghitung jumlah huruf yang muncul dari suatu kalimat :


?

1
2
3
4

<?php
$str = str_replace("dia", "", "dia itu pak RT. diaa itu jago matematika. Dia orangny
echo $count; // 2
?>

2) STR_IREPLACE()
Mengganti huruf/kata dari suatu kalimat, bersifat case insensitive.
a) Mengganti kata :
?

1
2
3
4
5
6
7

<?php
$text = "Hello world";
echo str_ireplace("world", "you", $text); // Hello you
$text1 = "Hello world";
echo str_ireplace("WORLD", "you", $text1); // Hello you
?>

b) Mengganti beberapa huruf sekaligus secara bersamaan :


?

1
2
3
4
5
6

<?php
$text = "Hello world";
$cari_huruf = array('HE', 'R', 'D');
$huruf_baru = array('se', 'y', 'e');
echo str_ireplace($cari_huruf, $huruf_baru, $text); // sello woyle
?>

c) Menghitung jumlah huruf yang muncul dari suatu kalimat :


?

1
2
3
4

<?php
$str = str_ireplace("dia", "", "DIA itu pak RT. DIAA itu jago matematika. Dia orangn
echo $count; // 3
?>

3) EXPLODE()
Memecah string / kalimat menjadi array. Fungsi ini bersifat case sensitive.
a) memecah kalimat berdasarkan spasi :
?

1
2
3
4
5
6
7
8
9

<?php
$text = "Hello world";
$pecah = explode(' ', $text);
echo $pecah[0]; // Hello
echo $pecah[1]; // world
$e = explode(' ', $text);
echo $e[0]; // Hello world
?>

b) memecah kalimat dengan limit :


?

1
2
3

<?php
$str = 'satu|dua|tiga';

4
5
6
7
8
9
10
11
12
13
14

$pecah = explode('|', $str, 2);


print_r($pecah); // Array ( [0] => satu [1] => dua|tiga )
echo $pecah[0]; // satu
echo $pecah[1]; // dua|tiga
$pecah1 = explode('|', $str, 3);
print_r($pecah1); // Array ( [0] => satu [1] => dua [2] => tiga )
echo $pecah1[0]; // satu
echo $pecah1[1]; // dua
echo $pecah1[2]; // tiga
?>

4) PREG_SPLIT()
Fungsi preg_split() mirip dengan explode(), yaitu memecah string ke dalam bentuk array. Fungsi
ini bersifat case sensitive / case insensitive, jadi bisa digunakan sesuai kebutuhan.
a) Memecah string yang case insensitive :
?

1
2
3
4
5
6
7
8

<?php
$text = "Hey, HARI ini hari libur";
$pecah = preg_split('/hari/i', $text);
print_r($pecah); // Array ( [0] => Hey, [1] => ini [2] => libur )
echo $pecah[0]; // Hey,
echo $pecah[1]; // ini
echo $pecah[2]; // libur
?>

Pola /hari/i akan sama dengan harI, haRi, atau HARI, atau hari dengan menambahkan
script i supaya case insensitive.
b) Memecah string berdasarkan pola angka :
?

1
2
3
4
5
6
7
8
9

<?php
$text = "0123456789";
$pecah = preg_split('/[0-2]/', $text);
print_r($pecah); // Array ( [0] => [1] => [2] => [3] => 3456789 )
echo $pecah[0]; //
echo $pecah[1]; //
echo $pecah[2]; //
echo $pecah[3]; // 3456789
?>

Pola [0-2] artinya akan memecah string dari angka 0,1 dan 2.
c) Memecah string berdasarkan pola huruf :
?

1
2
3
4
5

<?php
$text = "0 A 1 b 2 C 3 d 4 E 5 f 6 g 7";
$pecah = preg_split('/[a-g]/i', $text);
print_r($pecah); // Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [
echo $pecah[0]; // 0
echo $pecah[1]; // 1

6
7
8
9
10
11
12
13

echo
echo
echo
echo
echo
echo
?>

$pecah[2];
$pecah[3];
$pecah[4];
$pecah[5];
$pecah[6];
$pecah[7];

//
//
//
//
//
//

2
3
4
5
6
7

Pola [a-g] artinya akan memecah string dari huruf a, b, c, d, e, f, g secara case insensitive.

5) SUBSTR()
Mengambil/memotong sebuah string berdasarkan posisi string.
?

1
2
3
4
5
6
7
8
9

<?php
$text = "ijklmno";
echo substr($text,
echo substr($text,
echo substr($text,
echo substr($text,
echo substr($text,
echo substr($text,
?>

1);
1, 3);
0, 6);
6, 6);
6, 8);
-3, 2);

//
//
//
//
//
//

jklmno
jkl
ijklmn
o
o
mn

6) PREG_REPLACE()
Mengganti huruf dari suatu kalimat berdasarkan pola yang sudah ditentukan. Pola ini biasa
disebut regular expression.
?

1
2
3
4
5
6
7
8

<?php
$text = "Halo? halo apa kabar?";
echo
echo
echo
echo
?>

preg_replace('/apa/', 'gimana', $text); // Halo? halo gimana kabar?


preg_replace('/\?/', '!', $text); // Halo! halo apa kabar!
preg_replace('/\?$/', '!', $text); // Halo? halo apa kabar!
preg_replace('/(Halo)\? \1/i', '\1', $text); // Halo apa kabar?

Mem-bold/menebalkan kata hasil pencarian (seperti pencarian di google) :


?

<?php
1$text = "Perkembangan teknologi informasi dan komunikasi berkembang sangat pesat di era
termasuk dunia pendidikan. Perubahan teknologi ini memicu dunia pendidikan untuk selalu
2perkembangan teknologi yang ada. Perkembangan penggunaan teknologi informasi dan komuni
3terjadinya perubahan paradigma dalam dunia pendidikan dari techer centered menjadi stud
4$keyword = "pesatnya perkembangan teknologi merubah paradigma pendidikan";
5$pattern = str_replace(' ', '|', $keyword);
6$replace = preg_replace("/$pattern/i", '<span style="font-weight:bold">\0</span>', $te

7
8echo $replace;
?>

Outputnya :

7) STRREV()
Fungsi ini digunakan untuk membalik (reverse) string.
?

1
2
3

<?php
echo strrev("Hello world!"); // !dlrow olleH
?>

8) STR_SHUFFLE
Fungsi ini digunakan untuk mengacak string secara random.
?

1
2
3

<?php
echo str_shuffle("Hello world"); // ldowHo rlle
?>

Install composer in windows


Posted on May 9, 2015 by Ambar Hasbiyatmoko

In this tutorial, we will installing composer in windows.

What is composer?

Composer is a tool for dependency management in PHP. It allows you to


declare the dependent libraries your project needs and it will install them in
your project for you.
( getcomposer.org )
So, with composer we can install any libraries php with dependencies.
Ok, The following are the steps :
1. Download and install composer for windows here https://getcomposer.org/doc/00intro.md#installation-windows
2. If during installation you see error The openssl extension is missing, you must activate
php_openssl in configuration php.
php_openssl used for allow https.
For activate, open php.ini,
find extension=php_openssl.dll, then remove ;
3. Restart your apache.
4. Install again your composer.
5. Finish.

Anda mungkin juga menyukai