0

I have code to separate text with punctuation.

$text = "I was eating at a restaurant.";
echo $text . "<br>";
$token = preg_replace('/([\.\,\(\)\'\"\!\?\:\;])/', " \\1", $text);
echo $token;

And the output like this

I was eating at a restaurant.

I was eating at a restaurant .

I want to ask, What is the meaning " \\1" in that replacement? I am still confused.

Can someone explain it?

Bibhudatta Sahoo
  • 4,179
  • 1
  • 22
  • 44
  • Whatever is in first capturing group `(...)`. – Tushar Jul 03 '17 at 07:30
  • [Ruby regex what does the \1 mean for gsub](//stackoverflow.com/q/15825872), [What's the meaning of a number after a backslash in a regular expression?](//stackoverflow.com/q/8624345) and [python regular expression "\1"](//stackoverflow.com/q/20802056) – Tushar Jul 03 '17 at 07:33

2 Answers2

0

Yes, that is referencing your capturing group(s) (...)

Fallenhero
  • 1,515
  • 1
  • 6
  • 17
0

If you want to split text, you can use preg_split().

\\1 mean, that you call to first capturing group - Simply first sentence in middle of ( and ).

If you have " \\1", you will see that it find "." and replace it with " ." - with dot with additional space before dot.

timiTao
  • 1,401
  • 3
  • 19
  • 34