PHP String Functions

By | August 30, 2020
PHP String Functions

Core PHP supports the string functions and it will require no installation for these functions to work. Below is the list of all string functions that you can use with PHP.

Functions

Addcslashes

This function will provide you with the backslash string

Vamware

Syntax

string addcslashes ( string $str , string $charlist )

Example

<?php
   echo addcslashes("Foo['.']", 'z..A');
?>

addslashes

This function will provide you with the string with blackslashes which is placed in front of the predefined characters

Syntax

string addslashes ( string $str )

Example

<?php
   $str = "Is your name sai Right?";
   echo addslashes($str);
?>

Bin2hex

This function will allow you to do the conversion from primary data to hexadecimal representation.

Syntax

string bin2hex ( string $str )

Example

<?php
   $binary = "XXX";
   $hex = dechex(bindec($binary));   
   echo $hex;
?>

Output

0

Chop

This function will allow you to remove the whitespace and the second parameter will specify the characters that need to be removed as well.

Syntax

chop(string,charlist)

Example

<?php
   $str = "XXX YYY!";
   echo chop($str,"YYY!");
?>

Output

XXX

Chr

This function will allow you to provide the specific characters

Syntax

string chr ( int $ascii )

Example

<?php
   echo chr(76) . "<br>"; 
   echo chr(78) . "<br>"; 
   echo chr(0x50) . "<br>"; 
?>

Output

L
N
P

chunk split

This function will allow you to split a string into chunks of chunklen provided and is separated with a specified end string.

Syntax

string chunk_split ( string $body [, int $chunklen = 76 [, string $end = "\r\n" ]] )

Example

<?php
   $str = "XXXYYY";
   echo chunk_split($str,1,".");
?>

Output

X.X.X.Y.Y.Y.

convert cyr string

This function will allow you to do the conversion from one Cyrillic character set to another

Syntax

string convert_cyr_string ( string $str , string $from , string $to )

Example

<?php
   $str = "www hello ";
   echo convert_cyr_string($str,'w','a'); 
?>

Output

www hello

convert uudecode

This function will allow you to decode an encoded string.

Syntax

string convert_uudecode ( string $data )

Example

<?php
   echo convert_uudecode("+22!L;W9E(%!(4\"$`\n`");
?>

Output

I love PHP!

count chars

This function will allow you to provide the information about the character that is used in a string

Syntax

mixed count_chars ( string $string [, int $mode = 0 ] )

Crc32

This function will allow you to calculate the 32-bit CRC for the provided string.

Syntax

crc32(string)

Example

<?php
   $str = crc32("XXX");
   printf("%u\n",$str);
?>

Output

2319231104

Crypt

This function will allow you to do the hashing for the provided string

Syntax

string crypt ( string $str [, string $salt ] )

Example

<?php
   $input = XXX;  
   $hash = crypt($input);
?>

Echo

This function will allow displaying the string or any character which is passed to this function. It can be more than one string. as one or more string

Syntax

void echo ( string $arg1 [, string $... ] )

Example

<?php
Echo (“hello”);
?>

Output

hello

Explode

This function will allow you to split the string with the help of another string and provide the result in the form of an array.

Syntax

array explode ( string $delimiter , string $string [, int $limit ] )

Example

<?php
   $str = "XXX YYYZZZ";
   print_r (explode(" ",$str));
?>

Output

Array
(
    [0] => XXX
    [1] => YYYZZZ
)

Fprintf

This function will allow you to write a formatted string to a stream. You can pass the stream parameter where you want to

Syntax

 int fprintf ( resource $stream , string $format [, mixed $args [, mixed $... ]] )

Example

<?php
   $f_name = 123;
   $file = fopen("index.txt","r");
   fprintf($file,"%f",$f_name);
?>

get html translation table

This function will provide you with the translation table which is used by htmlspecialchars() and htmlentities() functions.

Syntax

array get_html_translation_table ([ int $table = HTML_SPECIALCHARS [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = "UTF-8" ]]] )

Example

<?php
   print_r (get_html_translation_table(HTML_DATA));
?>

Hebrev

This function will allow you to do the conversion of provided logical Hebrew text to visual text

Syntax

string hebrev ( string $hebrew_text [, int $max_chars_per_line = 0 ] )

Example

<?php
   print_r (hebrev(HTML_SPECIALCHARS));
?>

Output

0

Hebrevc

This function will allow you to do the conversion from specified logical Hebrew text to visual text along with newline conversion

Syntax

string hebrevc ( string $hebrew_text [, int $max_chars_per_line = 0 ] )

Example

<?php
   print_r (hebrevc(HTML_SPECIALCHARS));
?>

Output

0

Hex2bin

This function will allow you to do the conversion from hexadecimal string to ASCII character

Syntax

string hex2bin ( string $data )

Example

<?php
   $hex = hex2bin("43480170");
   var_dump($hex);
?>

Output

string(4) "CHp"

html entity decode

This function will allow you to do the conversion from HTML entities to their application characters.

Syntax

string html_entity_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") ]] )

Example

<?php
   $input_string = "xxx \"point\" simple <b>pattern</b> learning";
   $ab = htmlentities($input_string);
   $b = html_entity_decode($ab);
   echo $ab;
?>

Output

xxx "point" simple <b>pattern</b> learning

html special chars decode

This function will allow you to do the conversion from special HTML entities back to characters.

Syntax

string htmlspecialchars_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 ] )

Example

<?php
   $input = "<p>data -> </p>\n";
   echo htmlspecialchars_decode($input);
?>

Output

data ->

Implode

This function will allow you to do Join array elements with a string.

Syntax

string implode ( array $pieces )

Example

<?php
   $a1 = array("1","2","3");
   echo "a1 is: '".implode("','",$a1)."'<br>";
   ?>

Output

a1 is: '1','2','3'

Join

This function is an alias of implode() function which returns a string from the elements of an array which is specified by the input parameter.

Syntax

join(separator,input)

Example

<?php
   $data = inputay('xxx','yyy','abc');   
   echo join(" ",$data);
?>

Output

xxx yyy abc

Lcfirst

This function will allow you to convert a string’s first character into lowercase.

Syntax

string lcfirst ( string $str )

Example

<?php
   $input_string = 'XXXYYY';
   $input = lcfirst($input_string);    
   echo $input;
?>

Output

xXXYYY

levenshtein

This function will allow you to calculate the Levenshtein distance between two strings

Syntax

 int levenshtein ( string $str1 , string $str2 )

Example

<?php
   echo 'distance is ';
   echo levenshtein("strings","duostrings");
?>

Output

distance is 3

localeconv

This function will provide you with the numeric formatting information

Syntax

array localeconv ( void )

Example

<?php
   setlocale(LC_ALL,"US");
   $demo = localeconv();
   print_r($demo);
?>

Output

Array ( [decimal_point] => . [thousands_sep] => [int_curr_symbol] => [currency_symbol] => [mon_decimal_point] => [mon_thousands_sep] => [positive_sign] => [negative_sign] => [int_frac_digits] => 127 [frac_digits] => 127 [p_cs_precedes] => 127 [p_sep_by_space] => 127 [n_cs_precedes] => 127 [n_sep_by_space] => 127 [p_sign_posn] => 127 [n_sign_posn] => 127 [grouping] => Array ( ) [mon_grouping] => Array ( ) )

ltrim

This function will allow you to trim the whitespace or other characters from the beginning of a provided string. The second parameter specifies the character that you want to trim.

Syntax

string ltrim ( string $str [, string $character_mask ] )

Example

<?php
   $str = "XXX YYY ZZZ!";
   echo ltrim($str,"demo");
?>

Output

XXX YYY ZZZ!

Md5_file

This function will allow you to provide the md5 hash calculation of a provided file

Syntax

string md5_file ( string $filename [, bool $raw_output = false ] )

Example

<?php
   $data = "demo.txt";
   $file = md5_file($data);
   echo $file;
?>

Md5

This function will allow you to calculate the md5 hash of a string

Syntax

string md5 ( string $str [, bool $raw_output = false ] )

Example

<?php
   $str = "XXXYYY";
   echo md5($str);
?>

Output

be8f96a5286fd728317f4f08b8588e70

Metaphone

This function will allow you to calculate the Metaphone key of a string

Syntax

string Metaphone ( string $str [, int $phonemes = 0 ] )

Example

<?php
   echo metaphone("Study simple");
?>

Output

STTSMPL

money format

This function will allow you to format a number as a currency string according to the provided format within the function.

Syntax

string money_format ( string $format , float $number )

Example

<?php
   $data = 257.10;
   setlocale(LC_MONETARY,"de_DE");
   echo money_format("%.2n", $data);
?>

Output

257.10

nl langinfo

This function will allow you to contain information about language and locale

Syntax

string nl_langinfo ( int $item )

Nl2br

This function will allow you to insert the HTML line breaks before all newlines in a string

Syntax

 string nl2br ( string $string [, bool $is_xhtml = true ] )

Example

<?php
   echo nl2br("xxx.\nAnother line."); 
?>

Output

xxx.
Another line.

number format

This function will allow you to format a given number with grouped thousands

Syntax

inputber_format(inputber,decimals,decimalpoint,separator)

Example

<?php
   $data = 1000.9;
   $f_data = inputber_format($data) ;
   echo $f_data;
   $f_data = inputber_format($data, 2);
   echo $f_data;
?>

Ord

This function will allow you to provide the ASCII Value of a provided character or string

Syntax

int ord ( string $string )

Example

<?php
   echo ord("hi")."<br>";
?>

Output

104

parse str

This function will allow you to parse the string into variables

Syntax

void parse_str ( string $str [, array &$arr ] )

Example

<?php
   parse_str("name1=Jacob Sons&age=45");
   echo $name1."<br>";
   echo $age;
?>

Output

Jacob Sons
45

Print

This function will allow you to provide the output with a string that is passed within the function.

Syntax

int print ( string $arg )

Example

<?php
   print "Hello"; 
?>

Output

Hello

printf

This function will provide you with the output a formatted string according to the format parameter.

Syntax

int printf ( string $format [, mixed $args [, mixed $... ]] )

Example

<?php
   printf("Study section");
?>

Output

Study section

quoted printable decode

This function will allow you to do the conversion of the quoted printable string to its corresponding 8-bit string

Syntax

string quoted_printable_decode ( string $str )

Example

<?php
   $input = "XXX = YYY.";
   echo quoted_printable_decode($input);
?>

Output

XXX = YYY.

quoted printable encode

This function will allow you to do the conversion from 8-bit string to the quoted printable string.

Syntax

string quoted_printable_encode ( string $str )

Example

<?php
   $input = "yyy = xxx.";
   echo quoted_printable_encode($input);
?>

Output

yyy =3D xxx.

quotemeta

This function will allow you to quote the metacharacters

Syntax

string quotemeta ( string $str )

Example

<?php
   $input = "study. and learning";
   echo quotemeta($input);
?>

Output

study\. and learning

Rtrim

This function will allow you to trim the white spaces from the end of the string

Syntax

string rtrim ( string $str [, string $character_mask ] )

Example

<?php
   $data = "hi bye";
   echo $data . "<br>";
   echo rtrim($data,"bye");
?>

Output

hi bye
hi

setlocale

This function will allow you to set the locale information

Syntax

string setlocale ( int $category , array $locale )

Example

<?php
   echo setlocale(LC_ALL,"UK");
   echo "<br>";
   echo setlocale(LC_ALL,NULL);
?>

Output

en_US.UTF-8

sha1 file

This function is used to calculate the sha1 hash of a file

Syntax

string setlocale ( int $category , array $locale )

Example

<?php
   $data = "demo.txt";
   $s_file = sha1_file($data);
   echo $s_file;
?>

Sha1

This function will allow you to calculate the sha1 hash of a string

Syntax

string sha1 ( string $str [, bool $raw_output = false ] )

Example

<?php
   $data = "hello";
   $s_file = sha1($data);
   echo $s_file;
?>

Output

aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d

similar text

This function will allow you to calculate the similarity between two strings

Syntax

 int similar_text ( string $first , string $second [, float &$percent ] )

Example

<?php
   echo similar_text("study","park");
?>

Output

0

soundex

This function will allow you to provide the soundex key of a string

Syntax

string Soundex ( string $str )

Example

<?php
   $data = "XXX";
   echo soundex($data);
?>

Output

X000

Sprintf

This function will allow you to format a string

Syntax

string sprintf ( string $format [, mixed $args [, mixed $... ]] )

Example

<?php
   $num = 123;
   $data = sprintf("%f",$num);
   echo $data;
?>

Output

123.000000

str getcsv

This function will allow you to parse a CSV string into an array.

Syntax

array str_getcsv ( string $input [, string $delimiter = ","  [, string $enclosure = '"' [, string $escape = "\\" ]]] )

Example

<?php
   $data = array_map('str_getcsv', file('Str.csv'));
?>

str ireplace

It is used to replace the characters with some other characters

Syntax

str_ireplace(find,replace,string,count)

Example

<?php
   $input = array("XXX","point","simple","easy");
   print_r(str_ireplace("simply","XXX",$input,$i)); // Case-insensitive   
   echo "<br>" . "Replacements: $i";  
?>

Output

Array ( [0] => XXX [1] => point [2] => simple [3] => easy )
Replacements: 0

str pad

It is used to pads a string to a new length.

Syntax

str_pad(string,length,pad_string,pad_type)

Example

<?php
   $input = "learning data ";
   echo str_pad($input,18,"!!"); 
?>

Output

learning data !!!!

str repeat

This function will allow you to repeat a string

Syntax

str_repeat(string,length,pad_string,pad_type)

Example

<?php
   echo str_repeat("XXX",5);
?>

Output

XXXXXXXXXXXXXXX
  • str replace

This function will allow you to replace the string present within another string

Syntax

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

Example

<?php
   echo str_replace("park","YYY","XXX park");
?>

Output

XXX YYY

str rot13

This function will allow you to perform the rot13 transform on a string

Syntax

string str_rot13 ( string $str )

Example

<?php

   echo str_rot13('PHP 7'); 

?>

Output

CUC 7

str shuffle

It is used to randomly shuffles a string

Syntax

string str_shuffle ( string $str )

Example

<?php
   $data = 'abcdefphp_function_str_shuffle';
   $var = str_shuffle($data);
   echo $var;
?>

Output

edhcsnfircunuthfe_spbfo_ftpa_l

str split

This function will allow you to do the conversion of a string to an array

Syntax

array str_split ( string $string [, int $split_length = 1 ] )

Example

<?php

   print_r(str_split("XXXYYY"));

?>

Output

Array ( [0] => X [1] => X [2] => X [3] => Y [4] => Y [5] => Y )

str word count

This function will allow you to provide the information about words used in a string

Syntax

mixed str_word_count ( string $string [, int $format = 0 [, string $charlist ]] )

Example

<?php
   echo str_word_count("simple study easy learning");
?>

Output

4

strcasecmp

This function will compare the two provided strings

Syntax

int strcasecmp ( string $str1 , string $str2 )

Example

<?php
   echo strcasecmp("Hello world!","study WORLD!");
?>

Output

-11
  • Strchr

This function will allow you to search for the first occurrence of a string within another given string.

Syntax

strchr(string,search,before_search);

Example

<?php
   echo strchr("Hello world!","world");
?>

Output

world!

Strcmp

This function will allow you to compare the two provided strings

Syntax

strcmp(string1,string2)

Example

<?php
  $var=  strcmp("Hello world!","Hello world!");
Echo $var;
?>

Output

0

Strcoll

This function will allow the two strings based on locale

Syntax

int strcoll ( string $str1 , string $str2 )

Example

<?php
     setlocale (LC_COLLATE, 'NL');
   $var=  strcoll("Hello World!","Hello World!");
Echo $var;
?>

Output

0

Strcspn

This function will allow you to return a number of characters of a string before any part of the specified characters is found.

Syntax

strcspn(string,char,start,length)

Example

<?php
   echo strcspn("XXX will simple easy learning","r");
?>

Output

24

strip tags

This function will allow you to strip the HTML and PHP tags from a string.

Syntax

string strip_tags ( string $str [, string $allowable_tags ] )

Example

<?php
   echo strip_tags("XXX <b><i>YYY</i></b>","<b>");
?>

Output

XXX YYY

Stripcslashes

This function will allow you to remove backslashes

Syntax

string stripcslashes ( string $str )

Example

<?php
   echo stripcslashes("Hello \World!");
?>

Output

Hello World!

Stripos

This function will allow you to find the position of the string’s first occurrence inside another string

Syntax

stripos(string,find,start)

Example

<?php
   echo stripos("java courses","java");
?>

Output

0

Stripslashes

It is used to un-quoted a quoted string

Syntax

string stripslashes ( string $str )

Example

<?php
   echo stripcslashes("XXX \YYY");
?>

Output

XXX YYY

Stristr

This function will allow you to find the first occurrence of a string inside another string.

Syntax

string stristr ( string $haystack , mixed $needle [, bool $before_needle = false ] )

Example

<?php
   echo stristr("sairam krishna","krishna",true);
?>

Output

sairam

Strlen

This function will allow you to find the provided string length.

Syntax

int strlen ( string $string )

Example

<?php
   $var = “hello”;
      echo strlen($var);
?>

Output

5

Strnatcasecmp

This function will allow you to compare the given two strings with a natural algorithm.

Syntax

strnatcasecmp(string1,string2)

Example

<?php
   echo strnatcasecmp("hwllo", "bye");
?>

Output

1

Strnatcmp

This function will allow you to compare the two strings with a natural order algorithm.

Syntax

int strnatcmp ( string $str1 , string $str2 )

Example

<?php
   echo strnatcmp("Jacob2", "sami1");
?>

Output

-1

strncasecmp

This function will compare the given strings. This function will take three parameters where the first two parameters are the strings and the third parameter will specify the number of characters to be matched from both the strings.

Syntax

strncasecmp(string1,string2,length)

Example

<?php
   echo strncasecmp("work!","thinking!",6);
?>

Output

3

Strncmp

This function will compare the first n characters of the given strings. This function will take three parameters where the first two parameters are the strings and the third parameter will specify the number of characters to be matched from both the strings.

Syntax

int strncmp ( string $str1 , string $str2 , int $len )

Example

<?php

   echo strncmp("work","working",6);

?>

Output

-2

strpbrk

It is used search’s a string for a specific character.

Syntax

string strpbrk ( string $haystack , string $char_list )

Example

<?php
   echo strpbrk("Input","I");
?>

Output

Input

Strpos

This function will allow you to find the string’s first occurrence position within another string.

Syntax

strpos(string,find,start)

Example

<?php
   echo strpos("simple learning easy !","easy");
?>

Output

16

Strrchr

This function will allow you to find the character of its last occurrence present within a provided string.

Syntax

strrchr(string,char)

Example

<?php

   echo strrchr("hello Jacob!","Jacob");

?>

Output

Jacob!

Strrev

This function will allow you to reverse a string.

Syntax

string strrev ( string $string )

Example

<?php
   echo strrev("study well");
?>

Output

llew yduts

Strripos

This function will allow you to find the last occurrence position of a provided string inside another provided string

Syntax

strripos(string,find,start)

Example

<?php
   echo strripos("work hard, HARD WORK","work");
?>

Output

16

Strspn

This function will allow you to find the number of characters present in the string specified by the charlist parameter.

Syntax

strspn(string,charlist,start,length)

Example

<?php
   echo strspn("Learning tutorial","t");
?>

Output

0

Strstr

This function will allow you to find the first occurrence of a given string

Syntax

strstr(string,search,before_search)

Example

<?php
   echo strstr("sam",111);
?>

Strtok

This function will split the string on the basis of the given split character.

Syntax

strtok(string,split)

Example

<?php
   $var = "simple courses";
   $var1 = strtok($var, " ");
   while ($var1 !== false){
      echo "$var1<br>";
      $var1 = strtok(" ");
   }
?>

Output

simple
courses

Strtolower

This function will allow you to convert the given string in lowercase.

Syntax

strstr(string,search,before_search)

Example

<?php
   echo strtolower("XXX YYY.");
?>

Output

xxx yyy

Strtoupper

This function will allow you to convert the string to the uppercase.

Syntax

string strtoupper ( string $string )

Example

<?php
   echo strtoupper("xxx yyy.");
?>

Output

XXX YYY

Strtr

This function will allow you to translate characters or replace a substring

Syntax

strtr(string,from,to)

Example

<?php
   echo strtr("sam","a","e");
?>

Output

sem

Substr_compare

This function will allow you to compare two given string format starting from a specific start position

Syntax

substr_compare(string1,string2,startpos,length,case)

Example

<?php
   echo substr_compare("work hard","work",2);
?>

Output

-1

Substr_count

This function will allow you to count the number of substrings present within the given string starting from the start position until the length is given.

Syntax

substr_count(string,substring,start,length)

Example

<?php
   echo substr_count("workhard","work");
?>

Output

1

Substr_replace

This function will allow you to replace the part of a string with another string

Syntax

substr_replace(string,replacement,start,length)

Example

<?php
   echo substr_replace("study simple","Study Simple",0);
?>

Output

Study Simple

Substr

This function will allow you to provide the substring of the given string.

Syntax

string substr ( string $string , int $start [, int $length ] )

Example

<?php
   echo substr("Simple Study",6);
?>

Output

Study

Trim

This function will allow you to remove the whitespaces and other characters

Syntax

trim(string,charlist)

Example

<?php
   $input = "simple study";
   echo trim($input,"study");
?>

Output-

imple

Ucfirst

This function will allow you to do the conversion only of the first character of a string to upper case

Syntax

ucfirst(string)

Example

<?php
   echo ucfirst("working");
?>

Output

Working

Ucwords

This function will allow you to do the conversion only of the first character of a string to upper case in each string

Syntax

ucwords(string)

Example

<?php
   echo ucwords("work");
?>

Output

Work

Vfprintf

This function will allow you to do the conversion of formatted string to a specific output

Syntax

vprintf(format,argarray)

Example

<?php
   $number = 22;
   $str = "Simple Study";
   vprintf("There are %u million users for %s.",array($number,$str));
?>

Output

There are 22 million users for Simple Study.

Vsprintf

This function will allow you to provide the formatted string

Syntax

string vsprintf ( string $format , array $args )

Example

<?php
   print vsprintf("%04d-%02d-%02d", explode('-', '2020-02-14'));
?>

Output

2020-02-14

Wordwrap

This function allows you to break the long words and arrange them to the next line

Syntax

wordwrap(string,width,break,cut)

Example

<?php
   $str = "simple study";
   echo wordwrap($str,5,"<br>\n",TRUE);
?>

Output

simpl
e
study

Leave a Reply

Your email address will not be published. Required fields are marked *