I'm trying to get pattern match for string like the following to convert every line into a list item <li>
:
-Give on result
&Second new text
-The third text
Another paragraph without list.
-New list here
In natural language: Match every string that starts with -
and ended with the new line sign \n
I tried the following pattern that works fine:
/^([-|-]\w+\s*.*)?\n*$/gum
Of course we can write it simply without the square brackets ^(-\w+\s*.*)?\n*$
but for debugging I used it as described.
In the example above, when I replaces the second -
with &
to be ^([-|&]\w+\s*.*)?\n*$
It works fine too and it mtaches the the second line of the smaple string. However, I could not able to make it matches -
prefixed with white space or suffixed with white space.
I changed the sample string to:
- Give on result
&Second new text
-The third text
Another paragraph without list.
-New list here
and I tried the following pattern:
/^([-|\- |&| -]\w+\s*.*)?\n*$/gum
However, it failed to match any suffixed or prefixed -
with white space.
Here are a live demo for the original working pattern:
[-|-]
doesn't make much sense.[]
defines a character class. e.g. "any of the characters inside the[]
can match a single character at one spot. so you're saying "is this char a dash or a pipe or a dash". – Marc Becho preg_replace('/^-(.*?)$/m', '<li>$1</li>', $string);
. Or should the content extend inside the sameli
if the next line doesnt have a-
? It would be useful to have what the output should be.. – chris85