PHP preg_match从javascript获取值

时间:2015-08-01 16:31:07

标签: javascript php preg-match

我正在尝试从javascript获取价值

$tt = '<script type="text/javascript">tabCls.push(
new pageModelTab(
"tabSpecifications"
, "/cusa/includeFile.action?productOverviewCid=0901e02480f8c511&componentCid=0901e024800bef11&userSelectedModel=0901e02480f8c511"
, "Specifications"
, 7
, false
, ""
, null
, true
, null
)
);
function onClick_tabSpecifications() {
try {
var location = new String(window.location);
if (location && location.indexOf("?selectedName") != -1) {
return true;
}
new TabState("7").addTabToBrowserHistory();
show("7");
showHideFooterDisclaimer(\'Specifications\');
return false;
} catch (e) {
//alert(e.message);
return true;
}
}
</script>';

function matchin($input, $start, $end){
        $in      = array('/');
        $out     =  array('\/');
        $startCh = str_replace($in,$out, $start);
        $endCh   = str_replace($in,$out, $end);

        preg_match('/(?<='.$startCh.').*?(?='.$endCh.')/', $input, $result);
        return array($result[0]);
    }

$matchin = matchin($tt,'tabSpecifications','Specifications');
echo $matchin[0];

我需要tabSpecifications和规范之间的价值

但我收到了错误 注意:未定义的偏移量:0 请帮忙

1 个答案:

答案 0 :(得分:1)

我想在这种情况下你只需要/tabSpecifications.*?Specifications/来匹配字符串。

<强>更新

很抱歉,我编写PHP代码已经很长时间了。

出现问题因为dot匹配所有字符,包括空格,但不匹配\n,我们应该使用[\\s\\S]来匹配包括\n在内的所有字符,或者只是添加sim到正则表达式。

<!-- language: lang-php -->

<?php

function matchin($input, $start, $end){
    $in      = array('/');
    $out     = array('\/');
    $startCh = str_replace($in, $out, $start);
    $endCh   = str_replace($in, $out, $end);

    $pattern = '/(?<='.$startCh.').*?(?='.$endCh.')/sim';
    // or you can use 
    // $pattern = '/(?<='.$startCh.')[\\s\\S]*?(?='.$endCh.')/';

    preg_match_all($pattern, $input, $result);
    return array($result[0]);
}

?>

参考文献: