Issue
Here is my code:
$template = preg_replace("\s*/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}", $template);
And the html
which I want to remove indentations of comment:
<!--{hello}-->
<div class="novalue">
<a>URL</a>
</div>
<!--{/hello}-->
I want it to be like this:
{hello}
<div class="novalue">
<a>URL</a>
</div>
{/hello}
But, the result was:
{hello}
<div class="novalue">
<a></a>
</div>{/hello}
My question is, why can't it just remove other space at the front of the current line ?
Solution
\s
includes linebreak, you want to use \h
for horizontal spaces instead And don't escape all characters in your regex, it becomes unreadable:
$html = <<<EOD
<!--{hello}-->
<div class="novalue">
<a>URL</a>
</div>
<!--{/hello}-->
EOD;
echo preg_replace('#\h*<!--({.+?})-->#', '$1', $html);
Output:
{hello}
<div class="novalue">
<a>URL</a>
</div>
{/hello}
Explanation:
# # regex delimiter
\h* # 0 or more horizontal spaces
<!-- # literally, begin comment
( # start group 1
{ # opening curly brace
.+? # 1 or more any character, not greedy
} # closing curly brace
) # end group 1
--> # literally, end comment
# # regex delimiter
Answered By - Toto Answer Checked By - Terry (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.