X
X
XY2016-03-08 09:32:18
PHP
XY, 2016-03-08 09:32:18

How to escape regex characters in multibyte strings?

I have a regular expression that contains external data, if a string containing service characters of a regular expression comes from external data, then this will show the whole pattern, I tried to find a built-in function for escaping characters in multibyte strings - I did not find it. How to escape regex characters?
Example:

function f(string $data)
{
    //Здесь сначала нужно экранировать символы в строке, но как это сделать?
    $data = screening($data);
    //Затем передать в паттерн регулярного выражения
    $pattern = "^{$data}+|{$data}+$";
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NightFall, 2016-03-17
@NightFall

The \Q and \E sequences escape the service characters between them. \Q - start, \E end escaping: \d\Q(some)\E\d - parentheses here are just parentheses
Apparently, $data = "\Q$data\E" should work (or "\\Q$ data\\E")
PS. just noticed that the question was asked 10 days ago... I guess you already found the answer...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question