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.
<?php $text = "This is Anne's book."; echo addslashes($text); ?>
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.
<?php $sentence = "My name is June"; $word = explode(" ", $sentence); print_r($word); ?>
 => My
 => name
 => is
 => 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.
<?php $arrayWord = array('This', 'is', 'a', 'sentence'); $joinString = implode(" ", $arrayWord); echo $joinString; ?>
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.
<?php $string = 'password'; $stringMd5 = md5($string); echo $stringMd5; ?>
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
<?php $text = '<b>June Susan</b> <i> June June</i>'; echo strip_tags($text); echo '<br>'; echo strip_tags($text, '<i>'); // Allow tag <i> ?>
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.
<?php $string = 'I am a superman'; echo strlen($string); ?>
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!
Hal yang juga menarik:
Semua skrip dan teknik dalam artikel di itx.web.id boleh digunakan sebagaimana kehendakmu tanpa perlu mencantumkan sumber. Kamu tidak boleh mengkopi seluruh artikel, dalam Bahasa Indonesia ataupun diterjemahkan ke dalam bahasa lain.