Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the commonly used character functions in php

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the commonly used character functions in php, which have a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

The details are as follows:

1. String substr (string $string, int $start [, int $length])

Function function: returns the string string the substring specified by the start and length parameters.

Parameters:

String: enter a string.

Start: if start is a non-negative number, the returned string will start at the start position of string and start at 0. For example, in 'abcdef', the position 0 is' aura 'and the position 2 is' c'

If start is negative, the return string will start the first character forward at the end of the string.

Returns FALSE if the length of string is less than or equal to start.

$rest=substr ("abcdef",-1); / return "f" $rest=substr ("abcdef",-2); / / return "ef" $rest=substr ("abcdef",-3prime1); / / return "d"

$length: if a positive number of length is provided, the returned string consists of up to length characters starting at start (depending on the length of the string).

If a negative length is provided, many characters at the end of the string will be left out, that is, characters will not be considered from the end (I can understand that I truncated), and if the start is not in the text, an empty string will be returned.

If a length with a value of 0pm false or NULL is provided, an empty string will be returned.

If no length is provided, the returned string will run from the position of the start to the end of the character.

$rest=substr ("abcdef", "0", "- 1"); / return abcde$rest=substr ("abcdef", "2", "- 1"); / / return cde$rest=substr ("abcdef", "4", "- 4"); / / return NULL$rest=substr ("abcdef", "- 3", "- 1"); / / return de

The parenthesis in [] indicates that the parameter is optional, and if it is not set, it is the default parameter. I have been in contact with php for almost 8 months. I have been wondering why the parameters of the function should be added with parentheses. Today, I can't help but Baidu! Ashamed!)

2. Int preg_match (string $pattern, string $subject [, array & $matches [, int $flag=0 [, int $offset=0])

Function function: search for a match between subject and the regular expression given by pattern.

Parameters:

$pattern: pattern to search for, character type.

$subject: enter a string.

$matches: if the parameter matches is provided, it will be populated with the search results, $matches [0] contains the text to which the full pattern matches, $matches [1] will contain the first capture subgroup matching to the text, and so on.

$flags: flags can be set to the following tag values:

PREG_OFFSET_CAPTURE

If this tag is passed, a string offset (relative to the target string) is appended to each occurrence of the match. Note: this changes the array populated to the matches parameter so that each element becomes a string to which the 0th element is matched, and the first element is the offset of the matching string in the target string subject.

Offset:

Usually, the search starts with the unknown beginning of the target string. The optional parameter offset is used to specify that the search starts from an unknown part of the target string (in bytes).

Note:

Using the offset parameter is different from passing the result of intercepting the target string by location through substr ($subject, $offset) to preg_match (), because pattern can contain assertions such as ^, $, or (?

There will be a match.

Array ([0] = > Array ([0] = > def [1] = > 0))

3. Strpos (string,find,start)

String: must specify the string to be searched.

Find: must specify the characters to look for.

Start: optional, specify where to start the search, starting with the first character by default.

This function is case-sensitive, and you can use the stripos () function if you don't want to.

Example:

Echo strpos ("Hello world!", "wo"); / / output 6

4. String dirname (string path)

Function function: get the directory name in the path. In Win32 systems, slashes (/) or backslashes (\) are fine; but the paths of other operating systems are (/).

$path = "/ etc/hostname"; $file = dirname ($path)

$file is "/ etc"

5. Str_split (string,length)

String is required. Specifies the string to be split.

Length is optional. Specifies the length of each array element. The default is 1.

Running result:

Array ([0] = > H [1] = > e [2] = > l [3] = > l [4] = > o) Thank you for reading this article carefully. I hope the article "what character functions are commonly used in php" shared by the editor will be helpful to you. At the same time, I also hope you will support us, pay attention to the industry information channel, and more related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 218

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report