Get rid of all punctuation in a string

I have an incoming string to be saved to the database, let’s say it’s:

She said, “I am going to London.”

There are different kinds of double quotes, apostrophes, commas, maybe even different kinds of full stops, so I think it is easiest it I just get rid of all punctuation and just leave the words, separated by a space. (I’ll do the same with the answer key.)

I think for this job PHP str_contains(string $haystack, string $needle): bool may be good, but I’d have to know all possible types of punctuation and have them in an array.

Maybe better with: ctype_alpha( $string [ $i ]) ), here I only want (English) alphabetical characters.

for ( $i = 0; $i < $length ; $i ++) {

if (! ctype_alpha( $string [ $i ]) ) {

delete the character;`

}

Anyway, I need to put together a function to check strings and remove punctuation, say

function sanitizeStrings($string) {

Can you help me please?

After a bit more research I found preg_replace().

How does this look? Seems to do the trick! I need to keep the | because, if there is more than 1 correct answer, that is my “or”.

function rmPunctuation($astring) {
$res = preg_replace("/[^a-zA-Z|\s]/", “”, $astring);
//echo 'result of removing punctuation looks like: ’ . $res;
//$studentAnswers[$num] = $res;
return $res;
} // end of function rmPunctuation

1 Like

Why are you changing the data?

I am changing the data because an apostrophe is not always the same, there are about 5 I believe, not to mention the other punctuation marks, which also have varieties. The students are using English input on Chinese keyboards.

I don’t want their attempts to be marked wrong simply because an apostrophe or double quote or comma is not the same as in my answer key.

But if you know a better way to do this, I’d be glad to hear it!

I would need more details on what you are doing. Sounds like an exam system. If so, why are they writing text answers?

Do you have your app available on a repo like Github?

No app, I don’t know much about any of this stuff!

We have to have online classes, because of the virus. I take exercises from the textbook and create webpages. Mostly multi-choice or ‘fill-in-the-gaps’. The latest Unit has a bit about changing reported speech to direct speech.

Simple example:
Change: “She said that she was going to London.” to direct speech.

She said, “I am going to London.”

If the students use a different comma, double quote or full stop, when PHP compares their answer with the answer key, they won’t get a point for that.

I would also add 0-9, other than that it looks good. You might also want to remove trailing spaces…

Sponsor our Newsletter | Privacy Policy | Terms of Service