PHP 12th week function learning record

Time:2021-1-23

sha1()

effect

sha1()Function to evaluate the value of a stringSHA-1Hash.

usage

sha1(string,raw)

case

<?php
$str = "Hello";
echo sha1($str);
?>

result

f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0

sha1_file()

effect

sha1_file()Function calculation fileSHA-1Hash.

usage

sha1_file(file,raw)

case

<?php
$filename = "test.txt";
$sha1file = sha1_file($filename);
echo $sha1file;
?>

result

aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d

similar_text()

effect

similar_text()Function to calculate the similarity between two strings.

usage

similar_text(string1,string2,percent)

case

<?php
echo similar_text("Hello World","Hello Peter");
?>

result

7

soundex()

effect

soundex()Function evaluates the Soundex key of a string.

usage

soundex(string)

case

<?php
$str = "Hello";
echo soundex($str);
?>

result

H400

sprintf()

effect

sprintf()Function to write the formatted string to a variable.

usage

sprintf(format,arg1,arg2,arg++)

case

<?php
$number = 9;
$str = "RUNOOB";
$txt = sprintf(% u million people visit% s every day! ", $str, $number);
echo $txt;
?>

result

RUNOOBEvery day9Ten thousand people are visiting!

sscanf()

effect

sscanf()Function parses input from a string according to the specified format.sscanf()Function parses a string into a variable based on a format string.

usage

sscanf(string,format,arg1,arg2,arg++)

case

<?php
$str = "age:30 weight:60kg";
sscanf($str,"age:%d weight:%dkg",$age,$weight);
// show types and values
var_dump($age,$weight);
?>

result

int(30) int(60)

str_getcsv()

effect

str_getcsv()Function analysisCSVFormat field and returns an array containing the fields read.

usage

str_getcsv(string,separator,enclosure,escape)
parameter describe
string Required. Specifies the string to parse.
separator Optional. Set the field separator (only one character is allowed). The default value is comma (,).
enclosure Optional. Set the field surround character (only one character is allowed). The default value is double quotation marks (“).
escape Optional. Set escape character (only one character is allowed). The default value is backslash (\).

str_ireplace()

effect

str_ireplace()Function to replace some characters in a string (case insensitive).

usage

str_ireplace(find,replace,string,count)

The function must follow the following rules:

  • If the string being searched is an array, it will return an array.
  • If the string being searched is an array, it will find and replace each element in the array.
  • If you need to find and replace an array at the same time, and the number of elements that need to be replaced is less than the number of elements found, then the redundant elements will be replaced with an empty string.
  • If you are looking up an array but replacing only one string, the replacement string will work on all the values found.

case

<?php
$find = array("HELLO","WORLD"); // This function is case-insensitive
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_ireplace($find,$replace,$arr));
?>

result

Array ( 
    [0] => B
    [1] => 
    [2] => ! 
)

str_pad()

effect

str_pad()Function to fill a string with a new length.

usage

str_pad(string,length,pad_string,pad_type)pad_ String is optional. Specifies the string used for padding. The default is blank.

pad_ Type possible values:

  • STR_ PAD_ Both – fills both sides of the string. If not even, the right side gets extra padding.
  • STR_ PAD_ Left – fills the left side of the string.
  • STR_ PAD_ Right – fills the right side of the string. This is the default.

case

<?php
$str = "Hello World";
echo str_pad($str,20,".",STR_PAD_LEFT);
?>

result

Hello World.........

str_repeat()

effect

str_repeat()Function repeats a string a specified number of times.

usage

str_repeat(string,repeat)

case

<?php
echo str_repeat(".",13);
?>

result

.............

str_replace()

effect

str_replace()Function to replace some characters in a string (case sensitive).

usage

str_replace(find,replace,string,count)

The function must follow the following rules:

  • If the string being searched is an array, it will return an array.
  • If the string being searched is an array, it will find and replace each element in the array.
  • If you need to find and replace an array at the same time, and the number of elements that need to be replaced is less than the number of elements found, then the redundant elements will be replaced with an empty string.
  • If you are looking up an array but replacing only one string, the replacement string will work on all the values found.

notes

The function is case sensitive. Please usestr_ireplace()Function to perform a case insensitive search

case

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

result

Hello Peter!

str_rot13()

effect

str_rot13()Function on a stringROT13code.

usage

str_rot13(string)

Tips

ROT13Coding is moving each letter forward in the alphabet13I got a letter. Numeric and non alphabetic characters remain unchanged.

case

<?php
echo str_rot13("Hello World");
echo "<br>";
echo str_rot13("Uryyb Jbeyq");
?>

result

Uryyb Jbeyq
Hello World

str_shuffle()

effect

str_shuffle()Function randomly scrambles all characters in a string.

usage

str_shuffle(string)

case

<?php
echo str_shuffle("Hello World");
?>

result

lolWo drHle

str_split()

effect

str_split()Function to split a string into an array.

usage

str_split(string,length)

case

<?php
print_r(str_split("Hello"));
?>

result

Array ( 
    [0] => H 
    [1] => e 
    [2] => l 
    [3] => l 
    [4] => o 
)

str_word_count()

effect

str_word_count()Function to count the number of words in a string.

usage

str_word_count(string,return,char)

case

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

result

2

strcasecmp()

effect

strcasecmp()Function to compare two strings.

usage

strcasecmp(string1,string2)

Tips

Tip: strcasecmp()The function is binary safe and case insensitive.strncasecmp()The function is similar, except that thestrncasecmp()You can specify the number of characters per string for comparison.

case

<?php
echo strcasecmp("Hello","HELLO");
echo "<br>";
echo strcasecmp("Hello","hELLo");
?>

result

0
0

strchr()

effect

strchr()Function to search for the first occurrence of a string in another string.

usage

strchr(string,search,before_search);before_ Search is optional. A default value is"false"Boolean value of. If set to"true", which returns the part of the string before the first occurrence of the search parameter.

notes

The function isstrstr()The alias of the function.stristr()Function.

case

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

result

world!

strcmp()

effect

strcmp()Function to compare two strings.

usage

strcmp(string1,string2)

notes

strcmp()Functions are binary safe and case sensitive.strncmp()The function is similar, except that thestrncmp()You can specify the number of characters per string for comparison.

case

<?php
echo strcmp("Hello","Hello");
echo "<br>";
echo strcmp("Hello","hELLo");
?>

result

0
-1

strcoll()

effect

strcoll()Function to compare two stringsA<A orA>a)。

usage

strcmp(string1,string2)

notes

strcoll()Is case sensitive, but not binary safe.CorPOSIXThen the function works in the same way as thestrcmp()Same.

case

<?php
setlocale (LC_COLLATE, 'NL');
echo strcoll("Hello World!","Hello World!");
echo "<br>";

setlocale (LC_COLLATE, 'en_US');
echo strcoll("Hello World!","Hello World!");
?>

result

0
0

strcspn()

effect

strcspn()Function returns the number of characters (including spaces) found in the string before any specified character is found.

usage

strcspn(string,char,start,length)

case

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

result

6

strcspn()

effect

strcspn()Function returns the number of characters (including spaces) found in the string before any specified character is found.

usage

strcspn(string,char,start,length)

case

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

result

6

strip_tags()

effect

strip_tags()Function to strip theHTMLXMLas well asPHPThe label of.

usage

strip_tags(string,allow)Allow is optional. Specify the label allowed. These tags will not be removed.

notes

The function always splitsHTMLnotes. This cannot be changed by the allow parameter.

case

<?php
echo strip_tags("Hello <b>world!</b>");
?>

result

Hello world!

stripcslashes()

effect

stripcslashes()Function delete byaddcslashes()Function.

usage

stripcslashes(string)

Tips

This function can be used to clean from the database or from theHTMLThe data retrieved from the form.

case

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

result

Hello World!

stripslashes()

effect

stripslashes()Function delete byaddslashes()Function.

usage

stripslashes(string)

Tips

This function can be used to clean from the database or from theHTMLThe data retrieved from the form.

case

<?php
echo stripslashes("Who\'s Peter Griffin?");
?>

result

Who's Peter Griffin?

stripos()

effect

stripos()Function to find the first occurrence of a string in another string (case insensitive)

usage

stripos(string,find,start)

Tips

stripos()Functions are case insensitive.

case

<?php
echo stripos("I love php, I love php too!","PHP");
?>

result

7

stristr()

effect

stristr()Function to search for the first occurrence of a string in another string.

usage

stristr(string,search,before_search)

Tips

The function is case insensitive. For case sensitive searches, use thestrstr()Function.

case

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

result

world!

strlen()

effect

strlen()Function to return the length of the string, Chinese string processing usemb_strlen()Function..

usage

strlen(string)

case

<?php
echo strlen("Hello");
?>

result

5

strnatcasecmp()

effect

strnatcasecmp()Function uses a"Nature"Algorithm to compare two strings (case insensitive).2Less than number10. In computer sorting,10less than2This is because10The first number in is less than2

usage

strlen(string)

Return value

  • 0 – if two strings are equal
  • < 0 – if string1 is less than string2
  • >0 – if string1 is greater than string2

case

<?php
echo strnatcasecmp("2Hello world!","10Hello WORLD!");
echo "<br>";
echo strnatcasecmp("10Hello world!","2Hello WORLD!");
?>

result

-1
1

strnatcasecmp()

effect

strnatcasecmp()Function uses a"Nature"Algorithm to compare two strings (case insensitive).2Less than number10. In computer sorting,10less than2This is because10The first number in is less than2

usage

strnatcasecmp(string1,string2)

Return value

  • 0 – if two strings are equal
  • < 0 – if string1 is less than string2
  • >0 – if string1 is greater than string2

case

<?php
echo strnatcasecmp("2Hello world!","10Hello WORLD!");
echo "<br>";
echo strnatcasecmp("10Hello world!","2Hello WORLD!");
?>

result

-1
1

strnatcmp()

effect

strnatcmp()Function uses a"Nature"Algorithm to compare two strings (case sensitive).

usage

strnatcmp(string1,string2)

Return value

  • 0 – if two strings are equal
  • < 0 – if string1 is less than string2
  • >0 – if string1 is greater than string2

case

<?php
echo strnatcmp("2Hello world!","10Hello world!");
echo "<br>";
echo strnatcmp("10Hello world!","2Hello world!");
?>

result

-1
1

strncasecmp()

effect

strncasecmp()Function to compare two strings (case insensitive).

usage

strncasecmp(string1,string2,length)

Return value

  • 0 – if two strings are equal
  • < 0 – if string1 is less than string2
  • >0 – if string1 is greater than string2

case

<?php
echo strncasecmp("Hello world!","hello earth!",6);
?>

result

0

strncmp()

effect

strncmp()Function to compare two strings (case sensitive).

usage

strncmp(string1,string2,length)

Return value

  • 0 – if two strings are equal
  • < 0 – if string1 is less than string2
  • >0 – if string1 is greater than string2

case

<?php
echo strncmp("Hello world!","Hello earth!",6);
?>

result

0

source

PHP 5 string function | rookie tutorial

This work adoptsCC agreementReprint must indicate the author and the link of this article