javascript - PHP preg_match 从 javascript 获取值

标签 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 和 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/

相关文章:

php - 警告 : preg_match() [function. preg-match]:未知修饰符

javascript - 更正移动正则表达式模式

javascript - 从网页上可能嵌套的 <span> 中提取所有文本

javascript - 对单个文件的多个 HTTP 请求

javascript - 检测具有无值属性的元素

php - 从字符串中获取前 100 个字符,尊重完整的单词

javascript - 在非 WebKit 浏览器中推迟 JavaScript 执行

php - MySQL 阻止用户获取其他用户的代码

php - CodeIgniter 中的当前 URI 段

php - PHP preg_functions 多字节安全吗?