字符串获取单个/多个值
string get single / multiple values
我有一个字符串
<P><A href="http://www.microsoft.com/downloads/details.aspx?FamilyId=39d8f0cd-4043-4eed-bd27-2f26748da664">Microsoft Word 2003 Service Pack 3</A> <BR>(2863866)</P>`
我需要从中得到 3 个值:
- $value1 = characters between " "
- $value2 = characters between "> </A
- $value3 = characters between ( )
知道如何实现吗?
试试正则表达式:
$string = '<P><A href="http://www.microsoft.com/downloads/details.aspx?FamilyId=39d8f0cd-4043-4eed-bd27-2f26748da664">Microsoft Word 2003 Service Pack 3</A> <BR>(2863866)</P>`'
$regex = '"(.*?)">(.*?)<\/A>.*?\((.*?)\)'
if($string -match $regex) {
$Matches[1]
$Matches[2]
$Matches[3]
}
http://www.microsoft.com/downloads/details.aspx?FamilyId=39d8f0cd-4043-4eed-bd27-2f26748da664
Microsoft Word 2003 Service Pack 3
2863866
这将要求所有元素始终以相同顺序存在(link,link 的名称,括号)
我有一个字符串
<P><A href="http://www.microsoft.com/downloads/details.aspx?FamilyId=39d8f0cd-4043-4eed-bd27-2f26748da664">Microsoft Word 2003 Service Pack 3</A> <BR>(2863866)</P>`
我需要从中得到 3 个值:
- $value1 = characters between " "
- $value2 = characters between "> </A
- $value3 = characters between ( )
知道如何实现吗?
试试正则表达式:
$string = '<P><A href="http://www.microsoft.com/downloads/details.aspx?FamilyId=39d8f0cd-4043-4eed-bd27-2f26748da664">Microsoft Word 2003 Service Pack 3</A> <BR>(2863866)</P>`'
$regex = '"(.*?)">(.*?)<\/A>.*?\((.*?)\)'
if($string -match $regex) {
$Matches[1]
$Matches[2]
$Matches[3]
}
http://www.microsoft.com/downloads/details.aspx?FamilyId=39d8f0cd-4043-4eed-bd27-2f26748da664
Microsoft Word 2003 Service Pack 3
2863866
这将要求所有元素始终以相同顺序存在(link,link 的名称,括号)