Angelo Angelo - 1 month ago 7
PHP Question

php regex to get string within string of md code block

I have this string:

```yaml
project_id: 524
type:
- content changes
- code changes
production_urls:
- http://produrl.net/special/page
database_tables:
- content
- articles
```


I need to get the text in between
yaml
and ```. So the new string would start with
project_id...
and end with
...articles


What is the most efficient way to get the new string with PHP?

Answer

With a regex, just do this:

 <?php


$string = '```yaml
project_id: 524
type:
  - content changes
  - code changes
production_urls:
  - http://produrl.net/special/page
database_tables:
  - content
  - articles
```';

$string = preg_replace('#^```yaml(.*)```$#s', '$1', $string);

echo "<pre>".print_r($string, true)."</pre>";

?>

Matt

Comments