Collins
Collins

Reputation: 1159

Regex Preg Replace HTML

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?

Upvotes: 2

Views: 87

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627607

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.

Upvotes: 1

delboy1978uk
delboy1978uk

Reputation: 12382

Turn it off then, simple!

CKEDITOR.config.autoParagraph = false;

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

Upvotes: 0

Related Questions