如何在php中的两个单词之间获取文本

时间:2015-12-09 15:24:57

标签: php

我有一个字符串:

message to="919434556709@s.whatsapp.net" type="text" id="R6P3LMzq29vk0" t="1449760775" notify="SG"&gt;<br />tx&nbsp;&nbsp;&nbsp;&nbsp;&lt;body&gt;&lt;/body&gt;<br />tx&nbsp;&nbsp;&lt;/message&gt;<br /><br />
rx&nbsp;&nbsp;&lt;message from="919788517593@s.whatsapp.net" type="text" id="jtOjHBip5NY+12" offline="0" t="1449674223" notify="Rohitashv singhal"&gt;<br />rx&nbsp;&nbsp;&nbsp;&nbsp;&lt;body&gt;Hiiiii&lt;/body&gt;<br />rx&nbsp;&nbsp;&lt;/message&gt;<br /><br />
tx&nbsp;&nbsp;&lt;receipt type="read" to="919788517593@s.whatsapp.net" id="jtOjHBip5NY+12" t="1449674223"&gt;&lt;/receipt&gt;<br /><br />
rx&nbsp;&nbsp;&lt;message from="919784517593@s.whatsapp.net" type="text" id="jtOjHBip5NY+13" offline="0" t="1449674223" notify="Rohitashv singhal"&gt;<br />rx&nbsp;&nbsp;&nbsp;&nbsp;&lt;body&gt;Hiiii&lt;/body&gt;<br />rx&nbsp;&nbsp;&lt;/message&gt;<br /><br />
tx&nbsp;&nbsp;&lt;receipt type="read" to="919894597593@s.whatsapp.net" id="jtOjHBip5NY+13" t="1449674223"&gt;&lt;/receipt&gt;<br /><br />
rx&nbsp;&nbsp;&lt;message from="91978451873@s.whatsapp.net" type="text" id="jtOjHBip5NY+12" offline="0" t="1449674223" notify="Rohitashv singhal"&gt;<br />rx&nbsp;&nbsp;&nbsp;&nbsp;&lt;body&gt;Hiiiii&lt;/body&gt;<br />rx&nbsp;&nbsp;&lt;/message&gt;<br /><br />
tx&nbsp;&nbsp;&lt;receipt type="read" to="919789817593@s.whatsapp.net" id="jtOjHBip5NY+12" t="1449674223"&gt;&lt;/receipt&gt;<br /><br />
rx&nbsp;&nbsp;&lt;message from="919784517593@s.whatsapp.net" type="text" id="jtOjHBip5NY+13" offline="0" t="1449674223" notify="Rohitashv singhal"&gt;<br />rx&nbsp;&nbsp;&nbsp;&nbsp;&lt;body&gt;Hiiii&lt;/body&gt;<br />rx&nbsp;&nbsp;&lt;/message&gt;<br /><br />
tx&nbsp;&nbsp;&lt;receipt type="read" to="919789917593@s.whatsapp.net" id="jtOjHBip5NY+13" t="1449674223"&gt;&lt;/receipt&gt;<br /><br />
rx&nbsp;&nbsp;&lt;message from="919784517999@s.whatsapp.net" type="text" id="jtOjHBip5NY+12" offline="0" t="1449674223" notify="Rohitashv singhal"&gt;<br />rx&nbsp;&nbsp;&nbsp;&nbsp;&lt;body&gt;Hiiiii&lt;/body&gt;<br />rx&nbsp;&nbsp;&lt;/message&gt;<br /><br />
tx&nbsp;&nbsp;&lt;receipt type="read" to="919004517593@s.whatsapp.net" id="jtOjHBip5NY+12" t="1449674223"&gt;&lt;/receipt&gt;<br /><br />
rx&nbsp;&nbsp;&lt;message from="919784517593@s.whatsapp.net" type="text" id="jtOjHBip5NY+13" offline="0" t="1449674223" notify="Rohitashv singhal"&gt;<br />rx&nbsp;&nbsp;&nbsp;&nbsp;&lt;body&gt;Hiiii&lt;/body&gt;<br />rx&nbsp;&nbsp;&lt;/message&gt;<br /><br />
tx&nbsp;&nbsp;&lt;receipt type="read" to="919700517593@s.whatsapp.net" id="jtOjHBip5NY+13" t="1449674223"&gt;&lt;/receipt&gt;<br /><br />
rx&nbsp;&nbsp;&lt;ib from="s.whatsapp.net"&gt;<br />rx&nbsp;&nbsp;&nbsp;&nbsp;&lt;offline count="6"&gt;&lt;/offline&gt;<br />rx&nbsp;&nbsp;&lt;/ib&gt;<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;

它只给出一个结果。我想在这两个词之间发生一切。我怎么能得到这个?

3 个答案:

答案 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"&gt;<br/>
rx&nbsp;&nbsp;&nbsp;&nbsp;&lt;body&gt;Hiiiii&lt;/body&gt;<br />
rx&nbsp;&nbsp;&lt;/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\"&gt;<br />tx&nbsp;&nbsp;&nbsp;&nbsp;&lt;body&gt;&lt;/body&gt;<br />tx&nbsp;&nbsp;&lt;/message&gt;<br /><br />\nrx&nbsp;&nbsp;&lt;message from=\"919788517593@s.whatsapp.net\" type=\"text\" id=\"jtOjHBip5NY+12\" offline=\"0\" t=\"1449674223\" notify=\"Rohitashv singhal\"&gt;<br />rx&nbsp;&nbsp;&nbsp;&nbsp;&lt;body&gt;Hiiiii&lt;/body&gt;<br />rx&nbsp;&nbsp;&lt;/message&gt;<br /><br />\ntx&nbsp;&nbsp;&lt;receipt type=\"read\" to=\"919788517593@s.whatsapp.net\" id=\"jtOjHBip5NY+12\" t=\"1449674223\"&gt;&lt;/receipt&gt;<br /><br />\nrx&nbsp;&nbsp;&lt;message from=\"919784517593@s.whatsapp.net\" type=\"text\" id=\"jtOjHBip5NY+13\" offline=\"0\" t=\"1449674223\" notify=\"Rohitashv singhal\"&gt;<br />rx&nbsp;&nbsp;&nbsp;&nbsp;&lt;body&gt;Hiiii&lt;/body&gt;<br />rx&nbsp;&nbsp;&lt;/message&gt;<br /><br />\ntx&nbsp;&nbsp;&lt;receipt type=\"read\" to=\"919894597593@s.whatsapp.net\" id=\"jtOjHBip5NY+13\" t=\"1449674223\"&gt;&lt;/receipt&gt;<br /><br />\nrx&nbsp;&nbsp;&lt;message from=\"91978451873@s.whatsapp.net\" type=\"text\" id=\"jtOjHBip5NY+12\" offline=\"0\" t=\"1449674223\" notify=\"Rohitashv singhal\"&gt;<br />rx&nbsp;&nbsp;&nbsp;&nbsp;&lt;body&gt;Hiiiii&lt;/body&gt;<br />rx&nbsp;&nbsp;&lt;/message&gt;<br /><br />\ntx&nbsp;&nbsp;&lt;receipt type=\"read\" to=\"919789817593@s.whatsapp.net\" id=\"jtOjHBip5NY+12\" t=\"1449674223\"&gt;&lt;/receipt&gt;<br /><br />\nrx&nbsp;&nbsp;&lt;message from=\"919784517593@s.whatsapp.net\" type=\"text\" id=\"jtOjHBip5NY+13\" offline=\"0\" t=\"1449674223\" notify=\"Rohitashv singhal\"&gt;<br />rx&nbsp;&nbsp;&nbsp;&nbsp;&lt;body&gt;Hiiii&lt;/body&gt;<br />rx&nbsp;&nbsp;&lt;/message&gt;<br /><br />\ntx&nbsp;&nbsp;&lt;receipt type=\"read\" to=\"919789917593@s.whatsapp.net\" id=\"jtOjHBip5NY+13\" t=\"1449674223\"&gt;&lt;/receipt&gt;<br /><br />\nrx&nbsp;&nbsp;&lt;message from=\"919784517999@s.whatsapp.net\" type=\"text\" id=\"jtOjHBip5NY+12\" offline=\"0\" t=\"1449674223\" notify=\"Rohitashv singhal\"&gt;<br />rx&nbsp;&nbsp;&nbsp;&nbsp;&lt;body&gt;Hiiiii&lt;/body&gt;<br />rx&nbsp;&nbsp;&lt;/message&gt;<br /><br />\ntx&nbsp;&nbsp;&lt;receipt type=\"read\" to=\"919004517593@s.whatsapp.net\" id=\"jtOjHBip5NY+12\" t=\"1449674223\"&gt;&lt;/receipt&gt;<br /><br />\nrx&nbsp;&nbsp;&lt;message from=\"919784517593@s.whatsapp.net\" type=\"text\" id=\"jtOjHBip5NY+13\" offline=\"0\" t=\"1449674223\" notify=\"Rohitashv singhal\"&gt;<br />rx&nbsp;&nbsp;&nbsp;&nbsp;&lt;body&gt;Hiiii&lt;/body&gt;<br />rx&nbsp;&nbsp;&lt;/message&gt;<br /><br />\ntx&nbsp;&nbsp;&lt;receipt type=\"read\" to=\"919700517593@s.whatsapp.net\" id=\"jtOjHBip5NY+13\" t=\"1449674223\"&gt;&lt;/receipt&gt;<br /><br />\nrx&nbsp;&nbsp;&lt;ib from=\"s.whatsapp.net\"&gt;<br />rx&nbsp;&nbsp;&nbsp;&nbsp;&lt;offline count=\"6\"&gt;&lt;/offline&gt;<br />rx&nbsp;&nbsp;&lt;/ib&gt;<br /><br />\n"; 

preg_match_all($re, $str, $matches);

print_r($matches);