Php 从匹配中提取字符串

Php 从匹配中提取字符串,php,preg-match-all,Php,Preg Match All,我是php新手,尝试使用preg_match_all从url提取数据 问题是匹配项被转换为字符串,我无法单独提取它们 $markup='使用吉布森美国 Les Test Paul Custom 1986 与卡勒工厂合作; $markup=preg_replace('~~si','$markup);//用空格替换 $markup=preg_replace(“~\\s+~”,“$markup);//将连续空间压缩为单个空间 if(preg_match_all(“~(.*?~si)”,$markup,

我是php新手,尝试使用preg_match_all从url提取数据

问题是匹配项被转换为字符串,我无法单独提取它们

$markup='使用吉布森美国
Les Test Paul Custom 1986

与卡勒工厂合作; $markup=preg_replace('~~si','$markup);//用空格替换
$markup=preg_replace(“~\\s+~”,“$markup);//将连续空间压缩为单个空间 if(preg_match_all(“~(.*?~si)”,$markup,$matches)){ //修剪深埃尼岩阵列 数组\u walk\u递归($matches,函数(&$match){ $match=修剪($match); }); //这将显示$matches的结构 列表($raw\u matches,$class\u matches,$internal\u matches)=$matches; //将类名与span内部值组合 变量转储(数组合并($matches[1],$matches[2]); } //这就是循环preg_match_all()结果的方式 foreach($key=>$raw\u match时匹配[0]as$key){ $class_match=$matches[1][$key]; $internal_match=$matches[2][$key]; 如果(!strcasecmp($class_match,'what YOU seek')){ echo$内部匹配; } }
不要使用正则表达式解析htmlpossible duplicate of You don't
(capture)
您的正则表达式中没有任何内容。如果您提供了一个示例,说明您试图解析的内容以及您希望提取的输出,那么我们可能现在就能够解决您需要正则表达式执行的操作-fir,这完全是错误的。你没有读symcbean吗?请读最后一句话。谢谢,但结果与我原来的正则表达式基本相同。也许我错了-communicating@TomSaget你试过了吗?它一点也不像你的。我基本上是在尝试这样做:我试图给一个变量分配一个键,因此使用他的输出示例,我将数组中的键
0
分配给
$new1
,这将等于
始终是动态文本a
, etc@TomSaget添加了更多代码,以演示如何循环
preg\u match\u all
结果。
preg_match_all()

Products: array(1) {
  [0]=> array(7) {
      [0] => string(46) "Product 1"
      [1] => string(42) "Product 2"
      [2] => string(46) "Product 3"
      [3] => string(41) "Product 4"
      [4] => string(58) "Product 5"
      [5] => string(42) "Product 6"
      [6] => string(37) "Product 7"
  }
}
$markup = '<span class="Products-Name">Used Gibson USA</span>
    <span class="Products-Discription">Les Test Test Test Paul Custom 1986
    <br />with Factory Kahler </span>';
$markup = preg_replace('~<br\\s*/?>~si', ' ', $markup); // replace <br> with space
$markup = preg_replace('~\\s+~', ' ', $markup); // compact consecutive spaces into a single space
if(preg_match_all('~<span class="Products-(.+?)">(.*?)</span>~si', $markup, $matches)){
    // trim the enite deep array
    array_walk_recursive($matches, function(&$match){
        $match = trim($match);
    });
    // this shows you how the $matches is structured
    list($raw_matches, $class_matches, $inner_matches) = $matches;
    // combine class names with span inner value
    var_dump(array_combine($matches[1], $matches[2]));
}
// this is how you loop preg_match_all() results
foreach($matches[0] as $key => $raw_match){
    $class_match = $matches[1][$key];
    $inner_match = $matches[2][$key];
    if(!strcasecmp($class_match, 'what YOU seek')){
        echo $inner_match;
    }
}