Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checks if a string ends with a specific word in php?

Tags:

regex

php

There are many questions like this question but I can not find exact answer. And I am unfamiliar Regular Expresion topic.

I wanted to know if $variable ends with "books".

For example :

$variable = "some-historical-books". 

So, I will display books page if it ends with books.

Thank you.

like image 560
kalaba2003 Avatar asked Nov 29 '22 13:11

kalaba2003


1 Answers

Maybe you should look into a tutorial. What you are looking for is an anchor, that marks the end of the string.

if(preg_match('/books$/', $variable))
    // redirect to books page
like image 100
Martin Ender Avatar answered Dec 05 '22 23:12

Martin Ender