haudenschilt
haudenschilt

Reputation: 171

PHP regex to find non-espaced letters

I want to replace F but not \F.

I've tried the following code, without any luck.

preg_replace("/[^\\]F/", "f", $str);

Upvotes: 2

Views: 228

Answers (2)

Jonah
Jonah

Reputation: 10091

This works.

$string = preg_replace('/([^\\\]|^)F/', '$1f', $string);

The reason there are three backslashes, is because the first one escapes the second one for the string, and that one escapes the last one for the regex. Here's a topic on another site about it: http://forums.devnetwork.net/viewtopic.php?t=125752

Update: Thanks to @Damp and @webbiedave

Upvotes: 2

Damp
Damp

Reputation: 3348

Try this :

preg_replace("/(?<!\\\)F/", "f", $str);

Upvotes: 6

Related Questions