2

Please consider the following paragraph...

{{if|service_name=Service}}
  Hello this is the Service text
{{endif}}

This is the format that I allow my users to place custom conditional variables inside contract text. This means that if the service matches "Service", the text will show. I achieve this replacement functionality with the following....

$text = preg_replace("/{{if\|service_name=$service}}\s*(.*?)\R{{endif}}/s", "$1", $text);

This works great, right up until CKeditor is used on the contract text field, and my clients new lines are wrapped in <p> tags.

So the above works, but this does not..,

<p>{{if|service_name=Service}}</p>

<p>Hello this is the Service text</p>

<p>{{endif}}</p>

I have placed an example here... https://www.phpliveregex.com/p/pEE

Can anyone shed some light on my issue?

3
  • 2
    1) Use another regex tester to test multiline strings as whole strings. 2) Remove \R since there is no line break between <p> and {{endif}} 3) Do not use U modifier that switches greediness. See regex101.com/r/kKt2M6/1 Commented Oct 23, 2018 at 8:32
  • So what is the result you expect? Note that you cannot "skip" the <p> tags when matching, you will have to remove them with some str_replace(['<p>', '</p>'], '', $res) or similar option (or strip_tags) after extraction. Commented Oct 23, 2018 at 8:49
  • @WiktorStribiżew Thank you. It was the \R newline that was tripping it up. Please add as an answer and I will accept :-) Commented Oct 23, 2018 at 8:52

2 Answers 2

1

The \R construct matches any line break sequence, and there is no line break between <p> and {{endif}}.

Your regex may be quick-fixed as

'~{{if\|service_name=Service}}\s*(.*?)\s*{{endif}}~s'
                                      ^^^

The \s* will match any amount of any whitespace (add u modifier after/before s if you may have any Unicode whitespace in the string).

See the regex demo.

Sign up to request clarification or add additional context in comments.

Comments

0

Turn it off then, simple!

CKEDITOR.config.autoParagraph = false;

https://docs-old.ckeditor.com/ckeditor_api/symbols/CKEDITOR.config.html#.autoParagraph

4 Comments

Nope, need the editor for all sorts of formatting options.
it's just auto paragraphs you're turning off, not all formatting, give it a try at least
Yeah that would be good, but the chances are they are going to use all sorts of other html in these areas, unordered lists, tables etc. I need something a bit more resilient. Thanks though
That should be fine though, I thought it was only HTML generated by CKEditor that the user did not actually input that you were trying to get rid of, I would presume any markup the user adds would be fine. Why not just try it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.