我正在解析一个 html 文件。我有一个大字符串,基本上是一个脚本。字符串看起来像这样:
productId":"12666","chooseText":"选择选项...","taxConfig":{"includeTax":true,"showIncludeTax":true,"showBothPrices":false,"defaultTax":19.6,"currentTax ":19.6,"inclTaxTitle":"Incl. 税"}}); var colorarray = new Array();
colorarray["c650"] = 'blush'; Event.observe('attribute698', 'change', function() { var colorId = $('attribute698').value; var attribute = 'attribute698'; var label = colorarray["c"+colorId]; if ($('attribute698').value != '') { setImages(attribute, colorId, label); } }); // var currentColorLabel = 'blush'; // var currentSku = '5010-4-n'; // var currentPosition = 'v'; // //
Event.observe(window, 'load', function() { //
setImages('attribute698', null, currentColorLabel); // });
我需要从第一个“(”到第一个“;”中提取内容。我尝试进行字符串提取但失败了。我尝试过预匹配但失败了。请告诉我一些解决我的问题的方法。以下是我尝试过的解决方案和问题。
$strScript = $tagscript->item(0)->nodeValue;
//this line returns empty string
$str_slashed = addslashes(trim($strScript) );
$pattern = '/\((.*);/';
preg_match($pattern,$str_slashed,$matches);
echo 'matches'."<br />";
var_dump($matches);
//Add slashes works only if I use it before assignment to other string
$matches = array();
$strScript = addslashes ($tagscript->item(0)->nodeValue);//. "<br />";
$pattern = '/\((.*);/';
preg_match($pattern,$strScript,$matches);
echo 'matches'."<br />";
var_dump($matches);
//str extract method
$posBracket = stripos ($strScript,'(');
echo $posBracket."<br />";
$posSemiColon = strpos ($strScript,';');
echo $posSemiColon."<br />";
$temp = mb_substr ($strScript,$posBracket ,($posSemiColon-$posBracket));
echo $temp."<br />";
上面的代码适用于小字符串
$strScript = "manisha( [is goo girl] {come(will miss u) \and \"play} ; lets go home;";
但不适用于长字符串。我该如何解决这个问题?请帮助我!