Example analysis of common string processing functions in php

  • 2021-08-05 09:25:25
  • OfStack

In this paper, the common string processing functions of php are described as examples. Share it for your reference. The specific analysis is as follows:

Here are only a few simple and commonly used functions:
chop performs a space removal process, get_html_translation_table returns a conversion list to a variable, defines a string htmlentities including HTML encoding, htmlspecialchars_decode defines a string including HTML special characters, nl2br quotemeta rtrim, etc.

Definition and usage: The chop () function deletes white space characters or other predefined characters from the end of the string, the alias of the rtrim () function.

Syntax: chop (string, charlist), code as follows:

$str="i'm a   teacher  ";          // Definition string  
$result=chop($str);           // Perform the stripping of spaces process
echo $result;            // Output result

Definition and usage: The get_html_translation_table () function returns the translation table used by the htmlentities () and htmlspecialchars () functions.

Syntax: get_html_translation_table (function, quotestyle), code as follows:


$trans=get_html_translation_table(html_entities);    // Returns a conversion list to a variable
print_r($trans);            // Output conversion table
$str="hallo & <frau> & krmer";         // Definition string
$encoded=strtr($str,$trans);         // Find character
echo $encoded;           // Output result
//
 
$str="a 'quote' is <b>bold</b>";       // Definitions include html Encoded string
echo htmlentities($str);        // Output a processed string
echo htmlentities($str, ent_quotes);     // Output with optional parameters
 
//
$str='<p>this -&gt; "</p>';        // Definition contains html String of special characters
echo htmlspecialchars_decode($str);     // Output the converted content
echo "<br>";
echo htmlspecialchars_decode($str,ent_noquotes);  // Output without encoding quotation marks
 
//
 
$str="cat isn't n dog";      // Defines a string containing a newline character
$result=nl2br($str);       // Perform a conversion operation
echo $result;        // Output the converted result
 
//
 
$str="hello world.(can you hear me?)";      // Defining a string containing metacharacters
$result=quotemeta($str);         // Perform a conversion operation
echo $result;           // Output the converted result
//
 
$str="hello world  ";          // Defines a string with a space at the end
$result=rtrim($str);          // Perform a conversion operation
echo $result;           // Output the converted result

I hope this article is helpful to everyone's php programming.


Related articles: