Question

e.g

string = "This is a re@lly long long,long! sentence";

becomes

string = "This is a long sentence";

Basically so all non-alphanumeric words or removed keeping spaces in tacked

Any ideas?

Was it helpful?

Solution

Try this one:

preg_replace("/(^|\\s)\\S*?[^ a-zA-Z0-9]\\S*?(\\s|$)/", '$1', $string)

OTHER TIPS

I think something like this is quite intuitive:

<?php

$text = "This is a #@^!%$ re@lly long long,long! sentence";
print preg_replace("/\\w*[^\\w\\s]\\w*\\s*/", "", $text);

?>

The output is (as seen on ideone.com):

This is a long sentence

This works by matching any sequence of \w* that is followed by [^\w\s] (neither a word character nor a whitespace), followed by any sequence of \w*\s*. Anything matching this can be deleted, so it's replaced with "".

See also

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top