Harinder
Harinder

Reputation: 1257

PHP preg_match to get value from javascript

I am trying ti get value from 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

But i am getting error Notice: Undefined offset: 0 please help

Upvotes: 2

Views: 474

Answers (1)

iplus26
iplus26

Reputation: 2647

I suppose that you just need /tabSpecifications.*?Specifications/ to match the string in this case.

Update:

Sorry, it's been a long time for me not writing PHP codes.

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.

<!-- 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:

Upvotes: 1

Related Questions