S
S
Sererator2011-04-07 14:57:11
PHP
Sererator, 2011-04-07 14:57:11

Php + regExp, how?

We have a simple example (in php):

$text='\\';
preg_match_all('/\\\\/',$text,$item);
print_r($item);

At the output we get:
Array
(
    [0] => Array
        (
            [0] => \
        )

)

The question is how to get 2 slashes instead of one? It is necessary not to adapt to the desired string, but somehow disable the escaping of the slash in php.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
[
[email protected]><e, 2011-04-07
@Serator

Or is it better to use var_dump instead of print_r?

R
Roman, 2011-04-07
@WNeZRoS

How are you going to get 2 slashes if there is only one in the input?
This is how you get two:

$text='\\\\';
preg_match_all('/\\\\+/',$text,$item);

A
Andrew, 2011-04-07
@Morfi

Do an
echo $text;

I
Ivan, 2011-04-07
@iSage

I came up with only a variant with preg_quote:


preg_match_all('/'.preg_quote('\\\\','/').'/',preg_quote($text,'/'),$item);

or, if you need slashes separately, then
preg_match_all('/\\\\/',preg_quote($text,'/'),$item);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question