regex - PHP extracting data from inbetween delimiters -
$heading = ''; $text = ' *heading 1* text **subheading 1a** more text **subheading 1b** text**subheading 1c** *heading 2* text **subheading 2a** more text **subheading 2b** more text**subheading 2c** '; if(preg_match('#*(+?)#*',$text,$result)); $headings .= $result; if(preg_match('#**(+?)#**',$text,$result)); $headings .= $result; echo $heading;
from $text, how possibly extract what's between *
in between 1 *
heading , whats between 2 **
subheading?
the output i'm trying achieve this:
heading 1 subheading 1a subheading 1b subheading 1c heading 2 subheading 2a subheading 2b subheading 2c
i did put great deal of effort, mind cannot think of anymore. can help?
try this:
$result = array(); if (preg_match_all('/([*]+[^*]+[*]+)/', $text, $matches)) $result = array_map(function ($v) { return str_replace('*', ' ', rtrim($v, '*')); }, $matches[1]); print_r($result);
Comments
Post a Comment