简体   繁体   English

PHP preg_match 从 javascript 获取值

[英]PHP preg_match to get value from javascript

I am trying ti get value from javascript我正在尝试从 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];

I need value between tabSpecifications and Specifications我需要 tabSpecifications 和 Specifications 之间的值

But i am getting error Notice: Undefined offset: 0 please help但我收到错误通知:未定义的偏移量:0 请帮忙

I suppose that you just need /tabSpecifications.*?Specifications/ to match the string in this case.我想你只需要/tabSpecifications.*?Specifications/在这种情况下匹配字符串。

Update:更新:

Sorry, it's been a long time for me not writing PHP codes.抱歉,我很久没有写 PHP 代码了。

Something went wrong because dot matches all characters including spaces, but not \\n , and we should use [\\\\s\\\\S] to match all the characters including \\n or simply add sim to the regular expression.出了点问题,因为 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]);
}

?>

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM