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.

$text = "This is Anne's book.";
echo addslashes($text);

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.

$sentence = "My name is June";
$word = explode(" ", $sentence);

Output :
[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.

$arrayWord = array('This', 'is', 'a', 'sentence');
$joinString = implode(" ", $arrayWord);
echo $joinString;

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.

$string = 'password';
$stringMd5 = md5($string);
echo $stringMd5;

Output :

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

$text = '<b>June Susan</b> <i> June June</i>';
echo strip_tags($text); echo '<br>';
echo strip_tags($text, '<i>'); // Allow tag <i>

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.

$string = 'I am a superman';
echo strlen($string); 

Output :

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! :D
Hal yang juga menarik:

Hak Cipta

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.

Related Articles:

Artikel terkait:


About the author

obviously, a girl. with sweet smile, off course. turning her 21 years trapped on Informatics departement and just started to having some crush with it lately. she uses wordpress. she loves php. she lo

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

x( X-( B-) ;-) :wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :D :?: :-| :-x :-o :-P :-D :-? :-)) :-) :-( :-& :) :( :!: 8-O 8-) 8) (Русский) (yahoo) (worship) (woot) (wave) (unsure) (tongue) (thinking) (tears) (taser) (smileydance) (sleeping) (sick) (scenic) (rofl) (rock) (party) (panic) (okok) (nottalking) (ninja) (music) (muscle) (muhaha) (money) (mmm) (lonely) (lol) (lmao) (idiot) (hungry) (highfive) (heart_beat) (heart) (headspin) (hassle) (haha) (gym) (griltongue) (goodluck) (girlkiss) (funkydance) (fish_hit) (eyeroll) (evilsmirk) (evil_grin) (drinking) (doh) (devil) (dance) (cry) (cozy) (coffee) (brokenheart) (bringit) (blush) (bigeyes) (beer) (banana_rock) (banana_ninja) (banana_cool) (applause) (annoyed) (angry) (K) (: