Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • String Function in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 208
    Comment on it

    String Functions

    String functions are the part of core PHP. These are used to manipulate string data. Following are some commonly used string functions used in PHP. These are:

    addslashes  : Adds slashes to specified characters (those characters are single quote ('), double quote ("), and the NULL character). This function is useful  to insert strings into a database.

    Syntax : 

    addslahes ('string')

    Example 1:

    print addslashes ('data');
    
    

    Result:  data

    In above example, nothing changes because the string does not contain single quote, double quote, or the NULL character.

    Example 2:

    
    print addslahes ('She said, "Good day!"');
    
    

    Result: She said, \"Good day!\"
     

    In above example , a backslash is added to the left of the double quotes.

    htmlentities  :  Used to convert characters into corresponding HTML entities where applicable. It is used to encode user input on a website so that users cannot insert harmful HTML codes into a site. 

    Syntax :

    htmlentities ('string', [quote_style], [character_set])

    [quote_style] is used to check whether to convert double quotes and single quotes.

    [character_set] is the character set to use.

    Example :

    
    print htmlentities('<br>An example');
    
    

    Result: &lt;br&gt;An example

    md5 function :  It is used to encrypt a string.

    Syntax :

    md5 ('string', [raw_output])

    Example :

    print md5('1');
    
    

    Result: c4ca4238a0b923820dcc509a6f75849b

     str_replace Function  :  It is  used to change certain occurrances on a string with a replacement string.

    Syntax :

    str_replace ('match_pattern', 'replacement_pattern', 'text', [count])

    where,

     match_pattern is the match to look for. 

    replacement_pattern is the replacement string to use.

    text is the initial string.

    count is the optional parameter and used to count the number of matches to replace.

    Example :

    $match_pattern = array("junior", "senior"); 
    $replacement_pattern = array("abc","xyz"); 
    $starting_text = "This senior is studying PHP."; 
    print str_replace ($match_pattern, $replacement_pattern, ,$starting_text);
    
    

    Result: This xyz is studying PHP.

    In above example, The word "senior" is the second element in the match array, which is replaced by the word "xyz", which is the second element in the replacement array.

    strlen Function  : It is used to return the length of a string. 

    Syntax :

    strlen ('string')

    Example :

    print strlen ('Strlen Function');
    
    


    Result: 15

    In the above example, spaces count towards string length just like a character does.

    strpos Function : It is used to find the position of the first match in a string. 

    Syntax :

    strpos ('string', 'match_pattern', [offset])

    where, offset parameter tells the function to start looking for the match after the offset-th character in the string. The value returned by the function still indicates the position of the first match relative to the entire string.

    Example 1:

    print strpos ('Hurrah Hurrah','Hur');
    
    
    
    
    
    

    Result: 0

    In this example, the match occurs at the very beginning of the string, hence the function returns 0.

    Example 2:

    
    print strpos ('Hurrah Hurrah', 'Hur', 3);
    
    
    
    

    Result: 7

    In above example, The offset value of 3 means that we start looking for the match in position 3. From there, the first match occurs at position 7.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: