Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:
66
Q:

regex - Remove words with specific ending from variable in PHP

I have a PHP variable that contains a long string, and I'm looking for a way to remove all words from it that have a specific ending.

For example, let's say my variable contains the following string: "I love eating delicious apples. They are so juicy and crisp."

Now, I want to remove all words from this string that end with "ous". So, in this case, the word "delicious" should be removed.

What regex pattern can I use in PHP to achieve this? Can someone please help me out? Thanks in advance!

All Replies

ralph75

I've encountered a similar situation before, and I found a solution using regular expressions in PHP to remove words with specific endings from a variable. Here's how I approached it:

To achieve this, you can use the `preg_replace()` function in PHP along with a regex pattern. In your case, you want to remove words ending with "ous". Assuming your variable is called `$text`, you can use the following code:

php
$text = "I love eating delicious apples. They are so juicy and crisp.";
$result = preg_replace("/\b\w+ous\b/", "", $text);

echo $result;


In this code, the regex pattern `/\b\w+ous\b/` is used. Let me explain how it works:

- `/\b` indicates a word boundary, making sure that we match complete words.
- `\w+` matches one or more word characters (letters, digits, or underscores) in the word.
- `ous\b` matches the specific ending "ous" of the word, with `\b` representing another word boundary.

By passing an empty string as the replacement parameter to `preg_replace()`, the words ending in "ous" will be removed, and the updated string will be stored in the `$result` variable.

After running the code, the output will be: "I love eating apples. They are so juicy and crisp." The word "delicious" has been successfully removed.

I hope this solution helps you achieve your desired outcome! Feel free to ask if you have any further questions or need more assistance.

rippin.serena

Hey there! I've faced a similar challenge while working with PHP, where I needed to remove words from a string based on a specific ending. Here's what worked for me:

Assuming your variable is called `$sentence` and you want to remove words ending with "tion", you can implement the following code:

php
$sentence = "I enjoy swimming in the ocean. The sensation is indescribable.";
$result = preg_replace("/\b\w+tion\b/", "", $sentence);

echo $result;


In this code, the regex pattern `/\b\w+tion\b/` is utilized. Here's how it operates:

- `/\b` represents a word boundary, ensuring that only complete words are targeted.
- `\w+` matches one or more word characters within the word.
- `tion\b` matches the specific ending "tion" of the word, with `\b` as the word boundary.

By replacing the matching words with an empty string in `preg_replace()`, the words ending in "tion" will be removed from the string. The updated version will be stored in the `$result` variable.

When you execute the code, you'll see the output: "I enjoy swimming in ocean. The is indescribable." As you can see, the word "sensation" has been successfully eliminated.

I hope this approach proves useful for your scenario! Feel free to ask if you have any more questions or need further assistance.

New to LearnPHP.org Community?

Join the community