Walter Robynson
Walter Robynson

Reputation: 69

Replace chars with preg_replace

Using PHP, I need to change this

^0123456789...$

to this

0123456789xxx

  1. extract optional ^ on the start.
  2. extract optional $ on the end of the string.
  3. replace all . for x.

Is it possible in only one ER?

Upvotes: 0

Views: 32

Answers (1)

Nick
Nick

Reputation: 147166

The simplest way to do this is with str_replace, no regex required:

$string = '^0123456789...$';
echo str_replace(array('.', '^', '$'), array('x', '', ''), $string);

Output:

0123456789xxx

Upvotes: 3

Related Questions