Search code examples
regexcharacterboundary

Boundary operator in regex PHP up to four occurrences of the character


<?php 
echo preg_replace('/(?<!\pL)(.){1,4}(?!\pL)/','%','Pamiętam i tak będę z tobą jestem jestem i czuweam dfgd....');
?>

How to do it, to if jestem will to have from 1 to 4 characters, to not output this?

((?<!\pL)(?!\pL)){1,4}

I want have result such:

Pamiętam jestem jestem czuweam dfgd....

Solution

  • You may use

    $s = 'Pamiętam i tak będę z tobą jestem jestem i czuweam dfgd.... Teraz idę do kina. A teraz nie idę.';
    echo preg_replace('/(?<!\S)\S{1,4}(?!\S)/u','%', $s);
    // => Pamiętam % % % % % jestem jestem % czuweam dfgd.... Teraz % % kina. % teraz % %
    

    Pattern details:

    • (?<!\S) - there must be a whitespace symbol or start of string immediately before...
    • \S{1,4} - 1 to 4 non-whitespace chars
    • (?!\S) - immediately after those 1-4 chars, there must be a whitespace symbol or end of string.

    See the PHP demo.

    Do not forget a /u UNICODE modifier since you are working with Unicode strings.