我正在尝试从 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 和 Specifications 之间的值
但是我得到了错误 注意: undefined offset :0 请帮忙
最佳答案
我想您只需要 /tabSpecifications.*?Specifications/
来匹配这种情况下的字符串。
更新:
对不起,我好久没写PHP代码了。
出现错误,因为点匹配所有字符包括空格,但不匹配 \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]);
}
?>
引用资料:
关于javascript - PHP preg_match 从 javascript 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31763774/