Vì chỉ có 1 giá trị số để cô lập trong chuỗi của bạn, tôi sẽ xác nhận và cá nhân sử dụng
$string = "In My Cart : 11 items";
echo [int]ltrim[$string, 'A..z: '];
// 11
1 với $string = "In My Cart : 11 items";
echo [int]ltrim[$string, 'A..z: '];
// 11
2.echo filter_var[$string, FILTER_SANITIZE_NUMBER_INT];
Một kỹ thuật whackier hoạt động bởi vì chỉ có 1 giá trị số và các ký tự duy nhất xuất hiện trước số nguyên là các chữ cái, dấu chấm hoặc không gian là sử dụng
$string = "In My Cart : 11 items";
echo [int]ltrim[$string, 'A..z: '];
// 11
3 với mặt nạ ký tự sau đó đúc chuỗi còn lại làm số nguyên.Thử nghiệm
$string = "In My Cart : 11 items";
echo [int]ltrim[$string, 'A..z: '];
// 11
Nếu vì một số lý do, có nhiều hơn một giá trị số nguyên và bạn muốn lấy cái đầu tiên, thì Regex sẽ là một kỹ thuật trực tiếp.
Thử nghiệm
echo preg_match['/\d+/', $string, $m] ? $m[0] : '';
$string = "In My Cart : 11 items";
echo [int]ltrim[$string, 'A..z: '];
// 11
Thử nghiệm
var_dump[sscanf[' ' . $string, '%*[^0-9]%d'][0]];
$string = "In My Cart : 11 items";
echo [int]ltrim[$string, 'A..z: '];
// 11
Trong bài viết này, chúng tôi sẽ trích xuất số từ các chuỗi bằng PHP. Có nhiều phương pháp tích hợp khác nhau để trích xuất số từ các chuỗi, một số trong số chúng được thảo luận dưới đây.
Methods:
- Sử dụng hàm preg_match_all [].preg_match_all[] Function.
- Sử dụng & nbsp; filter_var [] hàm.filter_var[] Function.
- Sử dụng & nbsp; hàm preg_replace [].preg_replace[] function.
Phương pháp 1: Sử dụng hàm preg_match_all [].Using preg_match_all[] Function.
Lưu ý: & nbsp; hàm preg_match [] được sử dụng để trích xuất số từ một chuỗi. Thông thường, tìm kiếm bắt đầu từ đầu chuỗi chủ đề. Độ lệch tham số tùy chọn được sử dụng để chỉ định vị trí từ nơi bắt đầu tìm kiếm. preg_match[] function is used to extract numbers from a string. Usually, the search starts from the beginning of the subject string. The optional parameter offset is used to specify the position from where to start the search.
Syntax:
int preg_match[ $pattern, $input, $matches, $flags, $offset ]
Giá trị trả về: Nó trả về đúng nếu một mẫu tồn tại, nếu không là sai.It returns true if a pattern exists, otherwise false.
Ví dụ 1: Ví dụ theo dõi các số nguyên bằng cách sử dụng hàm preg_match_all []. Thefollowing exampleextracts Integer Numbers using the preg_match_all[] function.
PHP
echo preg_match['/\d+/', $string, $m] ? $m[0] : '';
0echo preg_match['/\d+/', $string, $m] ? $m[0] : '';
1 echo preg_match['/\d+/', $string, $m] ? $m[0] : '';
2echo preg_match['/\d+/', $string, $m] ? $m[0] : '';
3echo preg_match['/\d+/', $string, $m] ? $m[0] : '';
4Các
var_dump[sscanf[' ' . $string, '%*[^0-9]%d'][0]];
2var_dump[sscanf[' ' . $string, '%*[^0-9]%d'][0]];
0var_dump[sscanf[' ' . $string, '%*[^0-9]%d'][0]];
1var_dump[sscanf[' ' . $string, '%*[^0-9]%d'][0]];
5Đầu ra
Array [ [0] => Array [ [0] => 2 [1] => 4 ] ]
Làm thế nào tôi có thể nhận được số từ chuỗi trong PHP?The following example extracts decimal numbers using the preg_match_all[] function.
PHP
echo preg_match['/\d+/', $string, $m] ? $m[0] : '';
0Phương pháp 1: Sử dụng hàm preg_match_all []. Lưu ý: hàm preg_match [] được sử dụng để trích xuất số từ một chuỗi ..
echo preg_match['/\d+/', $string, $m] ? $m[0] : '';
5int preg_match[ $pattern, $input, $matches, $flags, $offset ]2
echo preg_match['/\d+/', $string, $m] ? $m[0] : '';
7echo preg_match['/\d+/', $string, $m] ? $m[0] : '';
1echo preg_match['/\d+/', $string, $m] ? $m[0] : '';
7var_dump[sscanf[' ' . $string, '%*[^0-9]%d'][0]];
0var_dump[sscanf[' ' . $string, '%*[^0-9]%d'][0]];
1var_dump[sscanf[' ' . $string, '%*[^0-9]%d'][0]];
2var_dump[sscanf[' ' . $string, '%*[^0-9]%d'][0]];
0var_dump[sscanf[' ' . $string, '%*[^0-9]%d'][0]];
1var_dump[sscanf[' ' . $string, '%*[^0-9]%d'][0]];
5Đầu ra
Array [ [0] => Array [ [0] => 2 [1] => 4.8 ] ]
Làm thế nào tôi có thể nhận được số từ chuỗi trong PHP?Using filter_var[] function.
Phương pháp 1: Sử dụng hàm preg_match_all []. Lưu ý: hàm preg_match [] được sử dụng để trích xuất số từ một chuỗi ..The filter_var[] function filters a variable with the specified filter. This function is used to both validate and sanitize the data.
Syntax:
filter_var[var, filtername, options]
Sử dụng hàm preg_match_all [] .. It returns the filtered data on success, or FALSE on failure.
Example:
PHP
Sử dụng hàm filter_var [] ..
echo preg_match['/\d+/', $string, $m] ? $m[0] : '';
1 echo preg_match['/\d+/', $string, $m] ? $m[0] : '';
2echo preg_match['/\d+/', $string, $m] ? $m[0] : '';
3echo preg_match['/\d+/', $string, $m] ? $m[0] : '';
4Các
Ví dụ 2: Ví dụ sau đây trích xuất các số thập phân bằng cách sử dụng hàm preg_match_all [].
var_dump[sscanf[' ' . $string, '%*[^0-9]%d'][0]];
5Đầu ra
The numbers are: 24
Làm thế nào tôi có thể nhận được số từ chuỗi trong PHP?Using preg_replace[] function.
Phương pháp 1: Sử dụng hàm preg_match_all []. Lưu ý: hàm preg_match [] được sử dụng để trích xuất số từ một chuỗi .. The preg_replace[] function is an inbuilt function in PHP that is used to perform a regular expression for search and replace the content.
Syntax:
preg_replace[ $pattern, $replacement, $subject, $limit, $count ]
Sử dụng hàm preg_match_all [] ..This function returns an array if the subject parameter is an array, or a string otherwise.
Example:
PHP
echo preg_match['/\d+/', $string, $m] ? $m[0] : '';
0Array [ [0] => Array [ [0] => 2 [1] => 4.8 ] ]7
echo preg_match['/\d+/', $string, $m] ? $m[0] : '';
1 echo preg_match['/\d+/', $string, $m] ? $m[0] : '';
22____23echo preg_match['/\d+/', $string, $m] ? $m[0] : '';
4filter_var[var, filtername, options]2
Array [ [0] => Array [ [0] => 2 [1] => 4 ] ]7
filter_var[var, filtername, options]4
filter_var[var, filtername, options]5
echo preg_match['/\d+/', $string, $m] ? $m[0] : '';
7__7777272721The numbers are: 240
Array [ [0] => Array [ [0] => 2 [1] => 4.8 ] ]7
Array [ [0] => Array [ [0] => 2 [1] => 4.8 ] ]1
Array [ [0] => Array [ [0] => 2 [1] => 4.8 ] ]2
Array [ [0] => Array [ [0] => 2 [1] => 4.8 ] ]3
The numbers are: 245
var_dump[sscanf[' ' . $string, '%*[^0-9]%d'][0]];
5Đầu ra
The numbers are: 24