php - 从包含模式的字符串中提取数字

标签 php

我有一个这样的字符串

4-1,4-1,,,1-1,,5-4,2-1,

我需要提取每对中的第一个数字。例如,(4 -1、4 -1、、、1 - 1、、5 - 4 , 2 -1 ,)

有谁知道我怎样才能实现这个目标?

最佳答案

$string = '4-1,4-1,,,1-1,,5-4,2-1,';

preg_match_all('/(\d+)-/', $string, $matches);
// search for 1 or more digits that are followed by a hyphen, and return all matches

print_r($matches[1]);

输出:

Array
(
    [0] => 4
    [1] => 4
    [2] => 1
    [3] => 5
    [4] => 2
)

关于php - 从包含模式的字符串中提取数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14432081/

相关文章:

php - 尝试将 SQL 结果导入数组 : Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted

php - 使用mysql确定流行标签?

php - PHP中字符串中的类常量

php - 无法获得变化

php - 友谊系统SQL结构与查询

PHP 日期 - 从格式 W-m-Y 获取给定周的开始和结束日期

php - 登录用户的IP地址、日期和时间

php - Flex Builder 4、ZendAMF 和数据服务 - 错误?

php - 从数据库中的每个用户检索最后插入的行

php - 我想使用单个变量将多条数据发送到数据库(使用 JS、PHP 和 AJAX)