php-regexHow to match the end of a string when using regex in PHP?
To match the end of a string when using regex in PHP, you can use the $ character. This character is used to indicate the end of the string. For example:
$string = 'This is a string';
$pattern = '/string$/';
if (preg_match($pattern, $string)) {
echo 'Match found!';
}
Output example
Match found!
The code above uses the $ character to match the end of the string. The $pattern variable contains the regular expression /string$/, which looks for the string string at the end of the string. The preg_match() function is then used to check if the pattern matches the string. If a match is found, the echo statement is executed.
Helpful links
More of Php Regex
- How to match a space using PHP regex?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to match a single quote in PHP regex?
- How to use PCRE in PHP regex?
- How to use PHP regex to match a JSON key value?
- How to match a double quote in PHP regex?
- How to get only numbers from a string using regex in PHP?
- How to remove non-printable characters using PHP regex?
- How to use PHP regex to match an IP address?
- How to use PHP regex to match letters, numbers and underscores?
See more codes...