0

If I have a string that begins foo and is 8 characters in length so looks like foo????? What pattern will I need to replace it using preg_replace()?

HamZa
  • 14,051
  • 11
  • 52
  • 72
andrew
  • 8,963
  • 7
  • 27
  • 58

3 Answers3

2
preg_replace('/foo.{5}/', '',  $string)

should do what you want.

Tom Macdonald
  • 6,113
  • 7
  • 38
  • 58
1

You can easily do that by matching:

/(foo)(.{5})/

and replacing with

''

Demo: http://regex101.com/r/fX7tV9

sshashank124
  • 29,826
  • 8
  • 62
  • 75
1
preg_replace('^/foo.{5}$/','',$string)

This should fit your needs.

Search in $string for

^  //Begin of the line
foo //Text your searching for
. //Some character
{5} //5 times the dot as character.
$ // end of line

and replace it with '' (second parameter)

Xatenev
  • 6,274
  • 3
  • 16
  • 42