如何从PHP中的定界符中提取内容 [英] How to extract content from delimiters in php

查看:95
本文介绍了如何从PHP中的定界符中提取内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找从定界符中提取php内容的方法.例如:

I'm looking to extract a content in php from delimiters. For example :

content useless
<--------->
Content to get
content to get
content to get
<--------->
content useles
content useless
<--------->
Content to get
content to get
content to get
<--------->
content useless
<--------->
Content to get
content to get
content to get
<--------->

现在我只有这个:

<?php
$myfile = file_get_contents("foo.txt");
$rows = explode("\n", $myfile);

foreach($row as $line => $data) {
    if ($data == "<--------->") {

    }
}
?>

我真的不知道如何找到定界符的第二个位置并检索内容.

I don't really know how to find the second position of my delimiter and retrieve the content.

我希望我能清楚自己! 问候和感谢.

I hope I got myself clear ! Greetings and thanks.

推荐答案

这将获得< -------->之间的所有块的内容作为定界符

This will give get the content of all blocks between <--------> as delimiter

$pattern = '/<-{9}>([\S\s]*?)<-{9}>/';

$subject = "
<--------->
Content to get
content to get
content to get
<--------->
Some extra stuff
<--------->
Content to get
content to get
content to get
<--------->";

$matches = array();
preg_match_all($pattern, $subject, $matches);



$allBlocks = $matches[1];


foreach($allBlocks as $block)
{
   var_dump(explode("\n", $block));

}

但是,在定义定界符时,您应该尝试更加结构化.

However, you should try to be more structured in defining delimiters.

因为内容的开头与内容的结尾相同. <--------->您稍后可能会在缩放代码或内容也包含<--------->

As the beginning of your content are the same as the end of content. <---------> you may be in troubles later when scaling your code or if the content also contain the <--------->

尝试实现一些XML标签,例如</-------->或自定义表单.

Try implementing some XML tags like </--------> Or a custom form.

## BOU ##东西\ n东西## EOU ##

AS BOU/EOU开始/有用的东西结束

AS BOU /EOU being Beggining/End of usefull stuff

这篇关于如何从PHP中的定界符中提取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆