首页 > 解决方案 > php使用字符数从字符串中获取id

问题描述

我有这样的字符串

https://example.com/offers/show?ID=d5589152-1ba7-4bab-b810-1d43e734575e&something=blabla&somethingelse=blabla2

有时字符串看起来不同

https://example.com/o/ABC/PP-Bin/4be93fd6-1380-4403-9945-0c5bc54d05e6?sName=Bam+2rnt

我想按字符数从这个字符串中提取 ID。每个 ID 有 8-4-4-4-12 个字符

标签: php

解决方案


试试下面的正则表达式:

$subject = "https://example.com/offers/show?ID=d5589152-1ba7-4bab-b810-1d43e734575e&something=blabla&somethingelse=blabla2";

preg_match("/[a-zA-Z0-9]{8}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{12}/", $subject, $matches);

print_R($matches);

推荐阅读