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
?>
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
?>
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
?>
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
?>
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
?>
1
2
3
<?php
$str = 'satu|dua|tiga';
4
5
6
7
8
9
10
11
12
13
14
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
?>
<?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
?>
What is composer?