Reputation: 355
I am trying to manipulate some sentences, and I can remove the non standard characters from a string, but is there any way to do this only if it is at the beginning or end of a string? To remove non standard characters I am using the following:
preg_replace("/[^A-Za-z0-9 ]/", '', $string);
I would like to change a string like this:
"* This is a sentence. --"
To be this:
"This is a sentence."
Upvotes: 0
Views: 27
Reputation: 1863
This isn't 100% robust, but this works:
preg_replace("/(?:^[^A-z0-9]+|[^.A-z0-9]+$)/", "", $input_lines);
Basically, it's replacing either anything at the beginning for the line that isn't A-z, 0-9 with '' (since there are no matching groups) or (|
) at the end anything after a full stop that isn't 0-9, A-z.
Upvotes: 2