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! 😀