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,671
  • 11
  • 54
  • 75
andrew
  • 9,313
  • 7
  • 30
  • 61

3 Answers3

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

should do what you want.

Tom Macdonald
  • 6,433
  • 7
  • 39
  • 59
1

You can easily do that by matching:

/(foo)(.{5})/

and replacing with

''

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

sshashank124
  • 31,495
  • 9
  • 67
  • 76
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,383
  • 3
  • 18
  • 42