-2

I have a string ryyyr #rrrr iituut iit #Google rrr #Tom65

In this string I am looking for all tags by regex /^#[A-Za-zА-Яа-я0-9_]{4,20}/

And I want to replace all found tags with the word penis, But for some reason nothing works

public static function findHashTags () {       
    $string = 'ryyyr #rrrr iituut iit #Google rrr #Tom65';
    $pattern = '/^#[A-Za-zА-Яа-я0-9_]{4,20}/';
    $replacement = 'penis';
    $a = preg_replace($pattern, $replacement, $string);
    return $a;
}
pavel
  • 24,015
  • 8
  • 38
  • 57

1 Answers1

0

In your regex you have ^, it just strings with # at the beginning passed.

Regex you're looking for is

/#[A-Za-zА-Яа-я0-9_]{4,20}/

Or another variant is shorter (isn't exact equivalent, I mean it'll do the work for you too).

#\S{4,20}
pavel
  • 24,015
  • 8
  • 38
  • 57