How to extract text in php using regex

2020-07-05 06:53发布

My Text :

12a49803-713c-4204-a8e6-248e554a352d_ Content-Type: text/plain; charset="iso-8859-6" Content-Transfer-Encoding: base64 DQrn0Ocg0dPH5MkgyszR6sjqySDl5iDH5OfoyuXq5A0KDQrH5OTaySDH5NnRyOrJIOXP2ejlySAx MDAlDQogCQkgCSAgIAkJICA= --_12a49803-713c-4204-a8e6-248e554a352d_ Content-Type: text/html; charset="iso-8859-6" Content-Transfer-Encoding: base64 PGh0bWw+DQo8aGVhZD4NCjxzdHlsZT48IS0tDQouaG1tZXNzYWdlIFANCnsNCm1hcmdpbjowcHg7 

I want to extract iso-8859-6

3条回答
家丑人穷心不美
2楼-- · 2020-07-05 07:02

The regex you are looking for is:

iso[^"]+

The php code you need is:

<?php
$subject='12a49803-713c-4204-a8e6-248e554a352d_ Content-Type: text/plain; charset="iso-8859-6" Content-Transfer-Encoding: base64 DQrn0Ocg0dPH5MkgyszR6sjqySDl5iDH5OfoyuXq5A0KDQrH5OTaySDH5NnRyOrJIOXP2ejlySAx MDAlDQogCQkgCSAgIAkJICA= --_12a49803-713c-4204-a8e6-248e554a352d_ Content-Type: text/html; charset="iso-8859-6" Content-Transfer-Encoding: base64 PGh0bWw+DQo8aGVhZD4NCjxzdHlsZT48IS0tDQouaG1tZXNzYWdlIFANCnsNCm1hcmdpbjowcHg7';
$pattern='/iso[^"]+/m';
if (preg_match($pattern, $subject, $match))
echo $match[0];
?>

The output is:

iso-8859-6
查看更多
何必那么认真
3楼-- · 2020-07-05 07:13

you could do: preg_match('/charset="([^"]+)"/',$string,$m); echo $m[1];


Edit: In case all need matching (prompted from other answer) modify like this:

preg_match_all('/charset="([^"]+)"/',$string,$m); print_r($m);

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-07-05 07:25

if you are interested in getting both matches (since you have 2 in the string) and iterate through them you should do something like this. also i used single quotes to not have to escape the quotes inside the regex. used ridgerunners suggestions aswell.

preg_match_all('/charset="([^"]+)"/', $subject, $result, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($result[0]); $i++) {
    # Matched text = $result[0][$i];
}
查看更多
登录 后发表回答