Programming123
Programming123

Reputation: 302

How can I replace single  's with a space but, not if there are multiple  s?

I assume a regular expression might do the trick but I haven't been able to come up with one that works. I have some fairly long strings in PHP that I need to clean up. In some cases,   appears in stead of a single space character and in other caes     (etc) appears. I'd like to replace all of the single   occurence with a space but leave the others in place so that the intending can be maintained.

Any thoughts? I presume a regular expression could be used here but I've been struggling with making one for for a while!

Upvotes: 5

Views: 1317

Answers (2)

abiessu
abiessu

Reputation: 1927

Use an explicit regular expression that matches (not- ) (not- ) and add the replacement as $1 $2 (match 1 space match 2). You may have to code not-  explicitly as ([^;]|[^p];|[^s]p;|[^b]sp;|[^n]bsp;|[^&]nbsp;).

Edit: While [negative] lookarounds may be useful (and certainly less total code), you may want to measure the speed of each approach. I have found that certain mechanisms in regular expressions can be painfully slow compared with others, although I cannot speak directly to the speed of lookarounds. If speed becomes an issue, you can skip the regular expressions and use a combination of strpos and substring operations and tests which are very often much faster than regular expressions, even if they are more cumbersome to create. I suggest this only because you have a very explicit string you are looking for; with less definite strings, regex is definitely the way to go.

For this instance (in pseudo-code), your string strpos search would be a simple as strpos($mystring, " ") and once you have found a match, call strpos($mystring, "  "). If the two index calls return the same value, you can skip this replacement and search the string after the indexed point (start your single   search after indexDoubleFound + 12, but start your double   search after indexDoubleFound + 6 to ensure that you don't miss any and you don't unintentionally replace).

Upvotes: 1

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

You must use a negative lookbehind and a negative lookahead to ensure that you don't have other   around.

$str = preg_replace('~(?<!&nbsp;)&nbsp;(?!&nbsp;)~i', ' ', $str);

More informations about lookarounds here.

Upvotes: 12

Related Questions