artyom.stv
artyom.stv

Reputation: 2164

PHP Perl-derivative regex question

In regular expressions we can escape special characters like: /\. - dot/

And what if I want to "escape" a substring: /Some **esc.{2}aped** phrase/ (to match this pattern "Some esc.{2}aped phrase") ? Is there any such character sequences (to replace **)?

PHP example. I have pattern

$pattern = "/Some unknown text {$here}: (\\d+)/";

with UTF8 string $here. And want to test UTF8 string $input with $here substituted "as is" (ignore special characters in $here):

preg_match( $pattern, $input, $matches );

Thanks

Upvotes: 0

Views: 90

Answers (1)

Linus Kleen
Linus Kleen

Reputation: 34632

If I'm reading this right, you want to embed an unknown string into a reqular expression.

You can use preg_quote() for this:

$unknown = "Some. Text";
$regex = '/Some unknown text '.preg_quote($unknown, '/').': (\d+)/u';

Also, for UTF-8 encoded regular expressions, you might want to use the u modifier to recognize UTF-8 character sequences.

Upvotes: 1

Related Questions