How can a PHP developer determine if a string ends with a specific pattern, such as "number x number"?
To determine if a string ends with a specific pattern like "number x number", a PHP developer can use the `preg_match` function with a regular expression pattern that matches the desired format. The regular expression pattern should check for a number followed by the letter 'x' and another number at the end of the string. If the pattern is found at the end of the string, the function will return true, indicating that the string ends with the specified format.
$string = "Lorem ipsum 10x20";
$pattern = "/\d+x\d+$/";
if (preg_match($pattern, $string)) {
echo "The string ends with the specified pattern.";
} else {
echo "The string does not end with the specified pattern.";
}
Keywords
Related Questions
- What are some best practices for efficiently working with attributes in PHP DOM to avoid performance issues or errors?
- What is the most efficient way to remove individual items from an array stored in a PHP session?
- How can PHP be used to calculate and differentiate the need for CGI-enabled computers and regular computers in a school setting?