Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 696843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:06:24+00:00 2026-05-14T03:06:24+00:00

Solution: strpos turned out to be the most efficient. Can be done with substr

  • 0

Solution:
strpos turned out to be the most efficient. Can be done with substr but that creates a temporary substring. Can also be done with regex, but slower than strpos and does not always produce the right answer if the word contains meta-characters (see Ayman Hourieh comment).

Chosen answer:

if(strlen($str) - strlen($key) == strrpos($str,$key))
    print "$str ends in $key"; // prints Oh, hi O ends in O

and best to test for strict equality === (see David answer)

Thanks to all for helping out.


I’m trying to match a word in a string to see if it occurs at the end of that string. The usual strpos($theString, $theWord); wouldn’t do that.

Basically if $theWord = "my word";

$theString = "hello myword";        //match
$theString = "myword hello";        //not match
$theString = "hey myword hello";    //not match

What would be the most efficient way to do it?

P.S. In the title I said strpos, but if a better way exists, that’s ok too.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:06:24+00:00Added an answer on May 14, 2026 at 3:06 am

    You can make use of strrpos function for this:

    $str = "Oh, hi O";
    $key = "O";
    
    if(strlen($str) - strlen($key) == strrpos($str,$key))
        print "$str ends in $key"; // prints Oh, hi O ends in O
    

    or a regex based solution as:

    if(preg_match("#$key$#",$str)) {
     print "$str ends in $key"; // prints Oh, hi O ends in O
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Solution: I've misinterpreted the example from SQL Books Online. Yes, that below the section
Solution to original problem (below) may have been discovered. I commented out <identity> ...
Solution: I had also moved ashx and asmx files at the same time as
This task has already been asked/answered, but I recently had a job interview that
Solution: I end up creatting a WCF that accepts a get/post request, then place
Solution (kinda): Turns out this impersonation with .NET's security only allows application-level access. Since
Solution -- WorkflowProject   -- Workflow1   -- Workflow2 -- WebProject (WAP)   -- App_Data     -- MyDatabase.vdb3
Solution for writing a program for keep secret photos. I have many secret photos
Solution: if you have the same problem, addElement() instead of addChild() is what did
Solution is called Portal which holds Areas/CMS/Login folder inside it. Login controller in CMS/Controllers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.