PHP-从父字符串中删除以特定字符开头和结尾的子字符串
我有两个文件,index.md和subPage.md。这是index.md文件:PHP-从父字符串中删除以特定字符开头和结尾的子字符串,php,Php,我有两个文件,index.md和subPage.md。这是index.md文件: --- Title: Index page Description: This is a sample description. Keywords: Those, are, keywords. Template: index --- # Hello! This is an Index.md file. 这是sub.md: --- Title: Sub Page --- # Again hello Lorem ip
---
Title: Index page
Description: This is a sample description.
Keywords: Those, are, keywords.
Template: index
---
# Hello!
This is an Index.md file.
这是sub.md:
---
Title: Sub Page
---
# Again hello
Lorem ipsum dolor sit amet.
在file index.php上,我得到了这些文件的内容:
$indexPage = file_get_contents(__DIR__ . '/index.md');
$subPage = file_get_contents(__DIR__ . '/subPage.md');
我需要删除每个文件中以---开头和结尾的代码段。问题是PHP文件必须检测以这三个字符开头和结尾的子字符串。我怎么做
提前感谢:)您可以使用regexp进行此操作,请尝试以下操作: 下面是一个快速代码示例:
<?php
$indexPage = file_get_contents(__DIR__ . '/index.md');
$subPage = file_get_contents(__DIR__ . '/subPage.md');
$pattern = '/^---\n[\w\W]+---\n/m';
$indexReplaced = preg_replace($pattern, '', $indexPage);
$subPageReplaced = preg_replace($pattern, '', $subPage);
var_dump($indexReplaced, $subPageReplaced);
您可以使用regexp进行此操作,请尝试以下操作:
下面是一个快速代码示例:
<?php
$indexPage = file_get_contents(__DIR__ . '/index.md');
$subPage = file_get_contents(__DIR__ . '/subPage.md');
$pattern = '/^---\n[\w\W]+---\n/m';
$indexReplaced = preg_replace($pattern, '', $indexPage);
$subPageReplaced = preg_replace($pattern, '', $subPage);
var_dump($indexReplaced, $subPageReplaced);
为了清楚起见,期望的字符串应该是什么?为了清楚起见,期望的字符串应该是什么?$pattern=preg\u replace($pattern,,$indexPage)$indexReplaced=preg_replace($pattern,,$indexPage)
对于未获得第一个返回车厢\n
@abkrim抱歉,我不理解code no在最后一个-----之后清洁返回车厢。使用此$pattern='/^-[^-]+-\n/m'代码>,结果开始于“#您好…”@abkrim我稍微改变了RegExp$pattern=preg\u replace($pattern,,$indexPage)$indexReplaced=preg_replace($pattern,,$indexPage)
对于未获得第一个返回车厢\n
@abkrim抱歉,我不理解code no在最后一个-----之后清洁返回车厢。使用此$pattern='/^-[^-]+-\n/m'代码>,结果从“#你好…”开始@abkrim我稍微更改了RegExp