开发者

Reading php files with special tags in php

开发者 https://www.devze.com 2023-03-01 02:46 出处:网络
I have a file which reads as follows <<row>> 1|test|20110404<</row>> <<row>> 1|te开发者_JAVA百科st|20110404<</row>>

I have a file which reads as follows

<<row>> 1|test|20110404<</row>>
<<row>> 1|te开发者_JAVA百科st|20110404<</row>>

<<row>><</row>> indicates start and end of line.I want to read line between this tags and also check whether this tags are present.


The first thing you need to do is locate the position of this "tag". The strpos() function does just that.

$tag_pos=strpos('<> 1|test|20110404<> <> 1|test|20110404<>', '<>');
if ($tag_pos===false) {
    //The tag was not found!
} else {
    //$tag_pos equals the numeric position of the first character of your tag
}

If these are truly lines, an efficient way to get them all is just to split on <>.

$lines=explode('<>', '<> 1|test|20110404<> <> 1|test|20110404<>');
$lines=array_filter($lines); //Removes blank strings from array

You could improve this by adding a callback function to the array_filter() call that uses trim() to remove any whitespace and then see if it is blank or not.


Edit: Great, I see that your "tags" were missing from your post. Since your start and end tags do not match, the code above will be of little use to you. Let me try again...

function strbetweenstrs($source, $tag1, $tag2, $casesensitive=true) {
    $whatsleft=$source;
    while ($whatsleft<>'') {
        if ($casesensitive) {
            $pos1=strpos($whatsleft, $str1);
            $pos2=strpos($whatsleft, $str2, $pos1+strlen($str1));
        } else {
            $pos1=strpos(strtoupper($whatsleft), strtoupper($str1));
            $pos2=strpos(strtoupper($whatsleft), strtoupper($str2), $pos1+strlen($str1));
        }
        if (($pos1===false) || ($pos2===false)) {
            break;
        }
        array_push($results, substr($whatsleft, $pos1+strlen($str1), $pos2-($pos1_strlen($str1))));
        $whatsleft=substr($whatsleft, $pos2+strlen($str2));
    }
}

Note that I haven't tested this... but you get the generally idea. There is probably a much more efficient way to go about doing it.


Creating your own format is not so hard, but creating a script to read it can be difficult. The advantage of using standardized formats is that most programming languages has support for them already. For example:

XML: You can use the simplexml_load_string() function and it can make you navigate easily through your content.

$str = "<?xml version="1.0" encoding="utf-8"?>
<data>
   <row>1|test|20110404</row>
   <row>1|test|20110404</row>
</data>";
$xml = simplexml_load_string($str);

Now you can access your data

echo $xml->row[0];
echo $xml->row[1];

i'm sure you get the idea, there is also a very good support for JSON (Javascript Object Notation) using the jsondecode() function; Check it on php.net for more details


i would suggest to use preg_match :-

 preg_match( '#<< row>>(.*)<< /row>>#', $line, $matches);

 if( ! empty($matches))
 {
  // line was found
  print_r( $matches[1] ); // will contain the content between the start and end row tags
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消