rotezecke
rotezecke

Reputation: 85

punctuation ereg_replace preg_replace

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

Upvotes: 0

Views: 588

Answers (1)

agent-j
agent-j

Reputation: 27953

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:]])+";

Upvotes: 1

Related Questions