1

I'll start off with some code, this is my current setup:

$search = [ '/\{\{\sSTRING\s\}\}/is', '/\{\{STRING\}\}/is' ]; $replace = [ '<b>TEST_STRING</b>', '<b>TEST_STRING</b>' ]; echo preg_replace( $search, $replace, "{{STRING}}" ); 

This would then output TEST_STRING, as wanted, but I'm using two REGEX statements to make this work, and I want this to work with strings like {{ STRING }} as well as {{STRING}} by only using a single REGEX.

I thought the statement would also ignore there being whitespace, but the \s statement specifically looks for anything relating to [\r\n\t\f\v ], is there an expression to use that will ignore whitespace as well as include it?

1

1 Answer 1

2

You can add an optional quantifier "?" after whitespace character "\s".

$search = '/\{\{\s?STRING\s?\}\}/is'; $replace = '<b>TEST_STRING</b>'; echo preg_replace( $search, $replace, "{{STRING}}" ); 

Hope this helps.

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

1 Comment

Thanks a lot! :) Great knowledge to have.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.