我有一个字符串:
message to="919434556709@s.whatsapp.net" type="text" id="R6P3LMzq29vk0" t="1449760775" notify="SG"><br />tx <body></body><br />tx </message><br /><br />
rx <message from="919788517593@s.whatsapp.net" type="text" id="jtOjHBip5NY+12" offline="0" t="1449674223" notify="Rohitashv singhal"><br />rx <body>Hiiiii</body><br />rx </message><br /><br />
tx <receipt type="read" to="919788517593@s.whatsapp.net" id="jtOjHBip5NY+12" t="1449674223"></receipt><br /><br />
rx <message from="919784517593@s.whatsapp.net" type="text" id="jtOjHBip5NY+13" offline="0" t="1449674223" notify="Rohitashv singhal"><br />rx <body>Hiiii</body><br />rx </message><br /><br />
tx <receipt type="read" to="919894597593@s.whatsapp.net" id="jtOjHBip5NY+13" t="1449674223"></receipt><br /><br />
rx <message from="91978451873@s.whatsapp.net" type="text" id="jtOjHBip5NY+12" offline="0" t="1449674223" notify="Rohitashv singhal"><br />rx <body>Hiiiii</body><br />rx </message><br /><br />
tx <receipt type="read" to="919789817593@s.whatsapp.net" id="jtOjHBip5NY+12" t="1449674223"></receipt><br /><br />
rx <message from="919784517593@s.whatsapp.net" type="text" id="jtOjHBip5NY+13" offline="0" t="1449674223" notify="Rohitashv singhal"><br />rx <body>Hiiii</body><br />rx </message><br /><br />
tx <receipt type="read" to="919789917593@s.whatsapp.net" id="jtOjHBip5NY+13" t="1449674223"></receipt><br /><br />
rx <message from="919784517999@s.whatsapp.net" type="text" id="jtOjHBip5NY+12" offline="0" t="1449674223" notify="Rohitashv singhal"><br />rx <body>Hiiiii</body><br />rx </message><br /><br />
tx <receipt type="read" to="919004517593@s.whatsapp.net" id="jtOjHBip5NY+12" t="1449674223"></receipt><br /><br />
rx <message from="919784517593@s.whatsapp.net" type="text" id="jtOjHBip5NY+13" offline="0" t="1449674223" notify="Rohitashv singhal"><br />rx <body>Hiiii</body><br />rx </message><br /><br />
tx <receipt type="read" to="919700517593@s.whatsapp.net" id="jtOjHBip5NY+13" t="1449674223"></receipt><br /><br />
rx <ib from="s.whatsapp.net"><br />rx <offline count="6"></offline><br />rx </ib><br /><br />
我想在单词message from
和/message
如果我使用以下代码:
function get_string_between($string, $start, $end){
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
$res = cleanupXML($data);
$parsed = get_string_between($res, 'message fro', "/message");
echo $parsed;
它只给出一个结果。我想在这两个词之间发生一切。我怎么能得到这个?
答案 0 :(得分:1)
使用正则表达式和php正则表达式函数。
preg_match_all('/message from(.+)\/message/', $string, $variable);
它将为您提供一个包含6个元素的数组。每个元素如下所示:
message from="919788517593@s.whatsapp.net" type="text" id="jtOjHBip5NY+12"
offline="0" t="1449674223" notify="Rohitashv singhal"><br/>
rx <body>Hiiiii</body><br />
rx </message
答案 1 :(得分:0)
这个怎么样?
function getBetween($content,$start,$end){
$r = explode($start, $content);
if (isset($r[1])){
$r = explode($end, $r[1]);
return $r[0];
}
return '';
}
getBetween($res, 'message fro', "/message");
答案 2 :(得分:0)
使用正则表达式和preg_match_all()
:
$re = "/message from=(.*)\\/message/";
$str = "message to=\"919434556709@s.whatsapp.net\" type=\"text\" id=\"R6P3LMzq29vk0\" t=\"1449760775\" notify=\"SG\"><br />tx <body></body><br />tx </message><br /><br />\nrx <message from=\"919788517593@s.whatsapp.net\" type=\"text\" id=\"jtOjHBip5NY+12\" offline=\"0\" t=\"1449674223\" notify=\"Rohitashv singhal\"><br />rx <body>Hiiiii</body><br />rx </message><br /><br />\ntx <receipt type=\"read\" to=\"919788517593@s.whatsapp.net\" id=\"jtOjHBip5NY+12\" t=\"1449674223\"></receipt><br /><br />\nrx <message from=\"919784517593@s.whatsapp.net\" type=\"text\" id=\"jtOjHBip5NY+13\" offline=\"0\" t=\"1449674223\" notify=\"Rohitashv singhal\"><br />rx <body>Hiiii</body><br />rx </message><br /><br />\ntx <receipt type=\"read\" to=\"919894597593@s.whatsapp.net\" id=\"jtOjHBip5NY+13\" t=\"1449674223\"></receipt><br /><br />\nrx <message from=\"91978451873@s.whatsapp.net\" type=\"text\" id=\"jtOjHBip5NY+12\" offline=\"0\" t=\"1449674223\" notify=\"Rohitashv singhal\"><br />rx <body>Hiiiii</body><br />rx </message><br /><br />\ntx <receipt type=\"read\" to=\"919789817593@s.whatsapp.net\" id=\"jtOjHBip5NY+12\" t=\"1449674223\"></receipt><br /><br />\nrx <message from=\"919784517593@s.whatsapp.net\" type=\"text\" id=\"jtOjHBip5NY+13\" offline=\"0\" t=\"1449674223\" notify=\"Rohitashv singhal\"><br />rx <body>Hiiii</body><br />rx </message><br /><br />\ntx <receipt type=\"read\" to=\"919789917593@s.whatsapp.net\" id=\"jtOjHBip5NY+13\" t=\"1449674223\"></receipt><br /><br />\nrx <message from=\"919784517999@s.whatsapp.net\" type=\"text\" id=\"jtOjHBip5NY+12\" offline=\"0\" t=\"1449674223\" notify=\"Rohitashv singhal\"><br />rx <body>Hiiiii</body><br />rx </message><br /><br />\ntx <receipt type=\"read\" to=\"919004517593@s.whatsapp.net\" id=\"jtOjHBip5NY+12\" t=\"1449674223\"></receipt><br /><br />\nrx <message from=\"919784517593@s.whatsapp.net\" type=\"text\" id=\"jtOjHBip5NY+13\" offline=\"0\" t=\"1449674223\" notify=\"Rohitashv singhal\"><br />rx <body>Hiiii</body><br />rx </message><br /><br />\ntx <receipt type=\"read\" to=\"919700517593@s.whatsapp.net\" id=\"jtOjHBip5NY+13\" t=\"1449674223\"></receipt><br /><br />\nrx <ib from=\"s.whatsapp.net\"><br />rx <offline count=\"6\"></offline><br />rx </ib><br /><br />\n";
preg_match_all($re, $str, $matches);
print_r($matches);