Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用正则表达式从字符串中提取信息_Php_Regex - Fatal编程技术网

Php 使用正则表达式从字符串中提取信息

Php 使用正则表达式从字符串中提取信息,php,regex,Php,Regex,我想使用正则表达式提取“软件”及其值“2”的信息。请查看PHP中的以下字符串 $str=”http://abcfastdirectory.com vshttp://www.weblinkstoday.com/detail/link-116406.htm ::-(1)**ABCFAST目录(1)**允许(1)**创建(1)**目录(1)**专业(1)**((1)**ABCFAST目录(1)**允许(1)**俱乐部(1)**联系人(1)**创建(2)**详细信息(1)**目录(4)**目录(3)**

我想使用正则表达式提取“软件”及其值“2”的信息。请查看PHP中的以下字符串

$str=”http://abcfastdirectory.com vshttp://www.weblinkstoday.com/detail/link-116406.htm ::-(1)**ABCFAST目录(1)**允许(1)**创建(1)**目录(1)**专业(1)**((1)**ABCFAST目录(1)**允许(1)**俱乐部(1)**联系人(1)**创建(2)**详细信息(1)**目录(4)**目录(3)**用于(3)**it(1)**我们的(1)**页面(1)**专业(2)**软件(2)**“

如何使用PHP中的正则表达式从上面的字符串中提取信息?

如果您仅在“软件(2)”之后,则应执行以下操作:

$software = (int)preg_replace('/.*software\((\d+)\).*/','$1',$str);
preg_match('/software\((?<value>\d+)\)/', $str, $m);
print $m['value'];
preg_match('/software\((?\d+)\)/',$str,$m);
打印$m[“值”];
但是,如果要匹配每个类似
()
的零件,可以使用以下选项:

preg_match_all('/(?<key>\w+)\((?<value>\d+)\)/i', $str, $m);
foreach ($m['key'] as $i => $key) {
     print $key.' => '.$m['value'][$i]."\n";
}
preg_match_all('/(?\w+)\((?\d+)\)/i',$str,$m);
foreach($m['key']作为$i=>$key){
打印$key.'=>'.$m['value'][$i]。“\n”;
}
试试这个:

$pattern = '/\*\*software\([0-9]+\)\*\*/';
preg_match($pattern, $str, $matches);

// your value will be stored in $matches[1] 

如果您正在寻找,您可以尝试此方法并查看结果:

<?php

$str = "http://abcfastdirectory.com vs http://www.weblinkstoday.com/detail/link-116406.htm ::-(1)**abcfastdirectory(1)**allows(1)**create(1)**directories(1)**professional(1)**((1)**abcfastdirectory(1)**allows(1)**club(1)**contact(1)**create(2)**details(1)**directories(4)**directory(3)**for(3)**it(1)**our(1)**page(1)**professional(2)**software(2)**";

$matches = array();

preg_match_all('#\*\*(.*?)\((\d+)\)\*\*#',$str,$matches, PREG_SET_ORDER );

print_r($matches);