0

I have this snippet of code from an old OsCommerce install

    $pattern = $this->attributes['SEO_REMOVE_ALL_SPEC_CHARS'] == 'true'
                    ?   "([^[:alnum:]])+"
                    :   "([[:punct:]])+";

I would like to modify the [:punct:] selector so it excludes the - dash.

next line of code is

$anchor = ereg_replace($pattern, '', strtolower($string));

which removes the previously found characters. how can I keep my dashes?

Thanks, Mario

EDIT

I think i got it:

$pattern = $this->attributes['SEO_REMOVE_ALL_SPEC_CHARS'] == 'true'
                    ?   "([^[:alnum:]])+"
                    :   "([^-a-zA-Z0-9[:space:]])+";

note: the dash has to come first. or, for underscores:

$pattern = $this->attributes['SEO_REMOVE_ALL_SPEC_CHARS'] == 'true'
                    ?   "([^[:alnum:]])+"
                    :   "([^a-zA-Z0-9_[:space:]])+";

I didn't figure out how to use negative lookaheads :(. Cheers. Mario

rotezecke
  • 85
  • 1
  • 11

1 Answers1

1

You'll probably need to make your own [characterset] instead of using [:punct:].

This one looks about right, but you'll need to verify it.

[^a-zA-Z0-9-\s]

This will replace anything that is not an (a-z) letter, digit, white space, or dash.

$pattern = $this->attributes['SEO_REMOVE_ALL_SPEC_CHARS'] == 'true'
            ?   "([^[:alnum:]])+"
            :   "[^a-zA-Z0-9-\s]+";

Edit: Old answer, that won't work because ereg doesn't support lookaround.

Try this negative lookahead (?!-):

$pattern = $this->attributes['SEO_REMOVE_ALL_SPEC_CHARS'] == 'true'
                ?   "([^[:alnum:]])+"
                :   "((?!-)[[:punct:]])+";
agent-j
  • 27,335
  • 5
  • 52
  • 79