Mihalcea Alexandru
Mihalcea Alexandru

Reputation: 7

Replace dot from 2 letters

I want something like this:

$name = str_replace(".", " ", $name);

but if I have this name:

Text.Text.text.text.Text.5.0.1

I want to remove the dots only from letters and from "t.5". Text must result:

Text Text text text Text 5.0.1

Upvotes: 1

Views: 64

Answers (1)

CodeBoy
CodeBoy

Reputation: 3300

Try preg_replace('/([^\d])\./', '$1 ', $name)

/([^\d])\./ matches a non-digit followed by a .. Replaces it with $1, the non-digit you found, followed by a space.

Edit: given refined requirement from comment below, that "." are removed only if there's a non-digit before OR after, answer is (requires 2 statements):

$name = preg_replace('/([^\d])\./', '$1 ', $name);
$name = preg_replace('/\.([^\d])/', ' $1', $name);

Upvotes: 1

Related Questions