I’m trying to replace all \n‘s sans that final one with \n\t in order to nicely indent for a recursive function.
This
that
then
thar
these
them
should become:
This
that
then
thar
these
them
This is what I have: preg_replace('/\n(.+?)\n/','\n\t$1\n',$var);
It currently spits this out:
This
that
then
thar
these
them
Quick Overview:
Need to indent every line less the first and last line using regex, how can I accomplish this?
After fixing a quotes issue, your output is actually like this:
Use a positive lookahead to stop that trailing
\nfrom getting eaten by the search regex. Your “cursor” was already set beyond it so only every other line was being rewritten; your match “zones” overlapped.Live demo.