Php 从字符串中提取特定数据
我有一个带描述的长字符串。我想从字符串中提取一些信息。但我真的不知道怎么做 这是字符串: 大陆连续体接触2自动/zomerband-195/55 R15 V85。 Eigenschappen EU bandenlabel:brandstofefficiÃntie:F,握杆操作nat wegdek:C,geluid:71dB,klasse:C1,geluidsklasse:2-bij www.tirendo.nl。Nu geen verzendkosten!直接杠杆式bij u thuis 比吉恩·蒙塔吉蓬特·纳尔·库兹·比宁1-4达根 我想检索以下结果: BrandStofefficentie=F,握力op natwegdek=C,geluid=71dB 我曾尝试使用explodes来提取它,但实际效果并不理想Php 从字符串中提取特定数据,php,string,extract,substr,Php,String,Extract,Substr,我有一个带描述的长字符串。我想从字符串中提取一些信息。但我真的不知道怎么做 这是字符串: 大陆连续体接触2自动/zomerband-195/55 R15 V85。 Eigenschappen EU bandenlabel:brandstofefficiÃntie:F,握杆操作nat wegdek:C,geluid:71dB,klasse:C1,geluidsklasse:2-bij www.tirendo.nl。Nu geen verzendkosten!直接杠杆式bij u thuis 比吉恩
有人能帮我看一下吗?如果字符串的格式一直完全相同,您可以这样做: preg_match('#(.*)brandstofefficientie: (.), grip op nat wegdek: (.), geluid: (.+)dB(.*)#ui', $inputString, $matches); echo 'brandstofefficientie: '.$matches[2].'; grip op nat wegdek: '.$matches[3].'; geluid: '.$matches[4].'dB'; preg#u match('#(.*)brandstofefficentie:(.),grip op nat wegdek:(.),geluid:(.+)dB(.*)#ui',$inputString,$matches); 回显“BrandStofefficentie:”.$matches[2];夹点操作nat wegdek:“.$匹配[3]”;geluid:'.$matches[4]。'dB'; 我的第一个(但未被接受)答案是 $result = preg_replace('#(.*)brandstofefficientie: (.), grip op nat wegdek: (.), geluid: (.+)dB(.*)#ui', 'brandstofefficientie = $2, grip op natwegdek = $3, geluid = $4dB.', $inputString); echo $result; $result=preg#u replace(“#(.*)BrandStofefficentie:(.)、grip op nat wegdek:(.)、geluid:(.+)dB(.*)#ui、'BrandStofefficentie=$2、grip op NATWEEGDEK=$3、geluid=$4dB.'、$inputString); 回声$结果;
它将按字面意思返回结果。我建议您创建一个以信息字符串为键的数组,然后执行regexp以获得结果。大概是这样的:
$text='Continental CONTIPREMIUMCONTACT 2 auto/zomerband - 195/55 R15 V85. Eigenschappen EU bandenlabel: brandstofefficiëntie: F, grip op nat wegdek: C, geluid: 71dB, klasse: C1, geluidsklasse: 2 - bij www.tirendo.nl. Nu geen verzendkosten! Directe levering bij u thuis of bij een montagepunt naar keuze binnen 1-4 dagen.=';
$explode=explode(':',$text);
$explode_last=explode(',',$explode[4]);
echo $string=$explode[1]."=".$explode[2]."=".$explode[3]."=".$explode_last[0];
$string = "Continental CONTIPREMIUMCONTACT 2 auto/zomerband - 195/55 R15 V85. Eigenschappen EU bandenlabel: brandstofefficientie: F, grip op nat wegdek: C, geluid: 71dB, klasse: C1, geluidsklasse: 2 - bij www.tirendo.nl. Nu geen verzendkosten! Directe levering bij u thuis of bij een montagepunt naar keuze binnen 1-4 dagen.";
$myInfo = array(
"brandstofefficientie",
"grip op nat wegdek",
"geluid"
);
$pattern = "/(" . implode("|", $myInfo) . ")\:\s+(.*?),/";
preg_match_all($pattern, $string, $match);
var_dump($match);
您可以对允许的标志使用一些变体,以便根据需要以不同的方式对结果进行排序,例如:
preg_match_all($pattern, $string, $match, PREG_SET_ORDER); //see PREG_SET_ORDER
来源:
另一种替代方法是使用,如:
$pattern=“/(?”。内爆(“|“,$myInfo)。”\:\s+(?*?),/”;
preg_match_all($pattern,$string,$match,preg_SET_ORDER);
foreach($match AS$result)
{
echo“{$result['info']}:{$result['value']}
”;
}
您可以使用以下regex preg\u match方法来检索所需的字符串值
$text = 'Continental CONTIPREMIUMCONTACT 2 auto/zomerband - 195/55 R15 V85. Eigenschappen EU bandenlabel: brandstofefficiëntie: F, grip op nat wegdek: C, geluid: 71dB, klasse: C1, geluidsklasse: 2 - bij www.tirendo.nl. Nu geen verzendkosten! Directe levering bij u thuis of bij een montagepunt naar keuze binnen 1-4 dagen.';
preg_match('/^.*?brandstofefficiëntie: (.*?),.*?grip op nat wegdek: (.*?),*.?geluid: (.*?),/',$text,$result);
现在,您可以分别在
$result[1]
、$result[2]
和$result[3]
中找到brandstofeffici«ntie、grip op nat wegdek和geluid的值。字符串的格式是否始终相同?什么信息是静态的,什么变化?您可以使用正则表达式来获取所需的内容,前提是字符串彼此相似time@Cornelius字符串每次都不一样。但是我想提取的部分总是一样的,但是可以用E或D代替C。其他文本可以改变。谢谢你的回答,但不幸的是,我没有得到我想要的结果。使用上面给出的代码,我只得到我试图再次从中提取数据的字符串。但我想从那个字符串中提取信息。似乎我误解了你的意图。编辑我的答案,现在您可以在$matches[2]到$matches[4]中获得想要的值
$text = 'Continental CONTIPREMIUMCONTACT 2 auto/zomerband - 195/55 R15 V85. Eigenschappen EU bandenlabel: brandstofefficiëntie: F, grip op nat wegdek: C, geluid: 71dB, klasse: C1, geluidsklasse: 2 - bij www.tirendo.nl. Nu geen verzendkosten! Directe levering bij u thuis of bij een montagepunt naar keuze binnen 1-4 dagen.';
preg_match('/^.*?brandstofefficiëntie: (.*?),.*?grip op nat wegdek: (.*?),*.?geluid: (.*?),/',$text,$result);