0

I am looking to run a test on a string to determine whether or not it meets certain condiditions. I would like to extract the last 4 characters of any given string and test them against a constant variable. Any Ideas on how to do this?

Ex:

[email protected]

I would like to know if the last four characters in the string are '.edu'

Thanks

1
  • are you dealing with only emails ? Commented Oct 20, 2012 at 15:29

2 Answers 2

5

You can use substr for single-byte and mb_substr for multi-byte strings:

$suffix = substr($str, -4);

The negative start parameter value specifies to start from the end of the string.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 because this user has 174k rep and so hes gotta be correct.
0

Depending on your needs, any of the following might work for you:

  • strrpos() - find last occurrence within a string
  • substr() - check the contents of a specific portion of a string
  • preg_match() - complex pattern matching

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.