dcoffey3296
dcoffey3296

Reputation: 2594

How to check if a string ends with punctuation in php?

I am parsing some text line by line and if a given line ends with any punctuation or a number I'd like to have a boolean return true.

Is regex the best way or should I iterate with an array of chars to match? Feels like the array would be far too big and costly...

Could someone help me with the regex if that is indeed a good way?

function ends_with_punctuation_or_num($string){
   // check if the string ends with punctuation or a number
    if (/* TODO */)
        return true;
    else
        return false;
}

Upvotes: 6

Views: 6344

Answers (5)

user428517
user428517

Reputation: 4193

I'm not sure I'd use a regex to do this. It's just personal preference, but a regex seems unnecessary and less readable (it's also slower, but that probably doesn't matter here). I'd rather drop whatever chars I'm looking for into a string and compare:

$chars = '.,;\'"0123456789';
$last = substr($string, -1);

if(strpbrk($last, $chars) !== false) {}

Obviously, use mb_substring() instead for Unicode.

Could also use is_numeric() instead of putting digits in the string. But I find this simpler, more readable, and easier to modify later.

Upvotes: 0

Toto
Toto

Reputation: 91430

The unicode property for punctuation is \p{P} or \p{Punctuation} for a number it's \pN.

In php you can use:

preg_match('/[\p{P}\p{N}]$/u', $string);

This will return true if the string ends with a punctuation or a digit.

Have a look at this site.

Upvotes: 5

Rysiu
Rysiu

Reputation: 104

You can use substr function or for utf-8 mb_substr

$last = substr($string, -1);

if($last == '.' OR is_numeric($last)) {]

But if you use UTF8 string i recommend you to use it like this

mb_internal_encoding('utf-8');
$last = mb_substr($string, -1);
if($last == '.' OR is_numeric($last)) {}

Upvotes: 0

Ron van der Heijden
Ron van der Heijden

Reputation: 15070

echo substr("abcdef", -1); // returns "f"

http://php.net/manual/en/function.substr.php

Upvotes: 1

Martin Ender
Martin Ender

Reputation: 44259

Put this into your if-check:

preg_match("/[0-9.!?,;:]$/", $string)

That will match a digit, or any of the given punctuation characters right before the end of the string ($). Add any other punctuation characters you want to regard as a match into the character class (the square brackets).

Upvotes: 17

Related Questions