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

  • SEARCH
  • Home
  • 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 7931741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:48:44+00:00 2026-06-03T20:48:44+00:00

I am totally new to preg_replace , but the code below removes the last

  • 0

I am totally new to preg_replace, but the code below removes the last word from my string:

preg_replace('/\W\w+\s*(\W*)$/', '$1', $var);

I am trying to modify it, so that it removes the last two words.

The only thing that I could think of was to replace $1 with $2, but this seems to not have any effect at all and was probably just dumb :/

The string in question looks kinda like this:

Lorem ipsum dolor sit amet. Source: LOREM

I would like to remove Source: LOREM

  • 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-06-03T20:48:46+00:00Added an answer on June 3, 2026 at 8:48 pm

    A simple regular expression could match a space, followed by any number of letters (or a colon), followed by a space, followed by any number of letters at the end of a string:

    $str = "Lorem ipsum dolor sit amet. Source: LOREM";
    $str = preg_replace( "/\s[a-z:]+\s[a-z]+$/i", "", $str );
    
    // Lorem ipsum dolor sit amet.
    echo $str;
    

    The expression broken down follows:

    \s       // Single space
    [a-z:]+  // Any letter, a to z, or a colon, 1 or more times
    \s       // Single space
    [a-z]+   // Any letter, a to z, 1 or more times
    $        // End of string
    

    Demo: http://codepad.org/G22LnDDY

    One other method would be to use explode to create an array of words, and remove the last two.

    $str = "Lorem ipsum dolor sit amet. Source: LOREM";
    $words = explode( " ", $str );
    array_splice( $words, -2 );
    
    // Lorem ipsum dolor sit amet.
    echo implode( " ", $words );
    

    Demo: http://codepad.org/6XwqvwuP

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm totally new to GreaseMonkey, but I'm trying to make a little script. //
I'm totally new in C, and I'm trying to do a little application that
I'm totally new to flex. I'm getting a build error when using flex. That
I´m totally new to Powershell and wanted to write a script that deletes all
I'm totally new to jQuery and Ajax, and I'm just confused trying to work
I'm totally new to C# and I'm having an error that I can't guess
I'm totally new to flash, and I'm trying to build a very simple app.
I'm totally new to Ruby but not to programming. All I did was going
Totally new to this! As the title says, I'm trying to serve a stream
I am totally new to this and trying to host the simplest WCF service

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.