How to remove exact match from string?How can I prevent SQL injection in PHP?PHP and EnumerationsDeleting an element from an array in PHPHow do I create a simple 'Hello World' module in Magento?How do I get a YouTube video thumbnail from the YouTube API?How to remove part of a string?Reference — What does this symbol mean in PHP?Remove new lines from string and replace with one empty spaceHow do I check if a string contains a specific word?Remove the last character from string
How to remove ambiguity: "... lives in the city of H, the capital of the province of NS, WHERE the unemployment rate is ..."?
How do I call a 6 digit Austrailian phone number with a US based mobile phone?
How make an image of my entire usb flash drive?
What are these funnel-looking green things in my yard?
Telephone number in spoken words
Why is the result of ('b'+'a'+ + 'a' + 'a').toLowerCase() 'banana'?
Is it possible to grow new organs through exposure to radioactivity?
Markov-chain sentence generator in Python
Can the IPA represent all languages' tones?
How do some PhD students get 10+ papers? Is that what I need for landing good faculty position?
How many samples should I use?
The cat exchanges places with a drawing of the cat
Simplification of numbers
The cat ate your input again!
Why did Saruman lie?
How are you supposed to know the strumming pattern for a song from the "chord sheet music"?
Why does tag require braces while frac doesn't?
How many British prisoners of war were taken by the Wehrmacht and how many died?
How should I deal with a potential date who wouldn’t take no for an answer?
How exactly are corporate bonds priced at issue
Is it okay for a ticket seller to grab a tip in the USA?
My cat is a houdini
Is there a command to install basic applications on Ubuntu 16.04?
What is a good class if we remove subclasses?
How to remove exact match from string?
How can I prevent SQL injection in PHP?PHP and EnumerationsDeleting an element from an array in PHPHow do I create a simple 'Hello World' module in Magento?How do I get a YouTube video thumbnail from the YouTube API?How to remove part of a string?Reference — What does this symbol mean in PHP?Remove new lines from string and replace with one empty spaceHow do I check if a string contains a specific word?Remove the last character from string
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
How i can replace/remove exact match word from string, an example string
$string = 'Hello world, command data test com';
How to remove com ( exact match from the string ) but to don't remove the com from command to
php
add a comment |
How i can replace/remove exact match word from string, an example string
$string = 'Hello world, command data test com';
How to remove com ( exact match from the string ) but to don't remove the com from command to
php
1
str_replace is removing com from command to, this not helping
– Matei Zoc
Mar 27 at 10:16
add a comment |
How i can replace/remove exact match word from string, an example string
$string = 'Hello world, command data test com';
How to remove com ( exact match from the string ) but to don't remove the com from command to
php
How i can replace/remove exact match word from string, an example string
$string = 'Hello world, command data test com';
How to remove com ( exact match from the string ) but to don't remove the com from command to
php
php
asked Mar 27 at 10:14
Matei ZocMatei Zoc
9302 gold badges8 silver badges13 bronze badges
9302 gold badges8 silver badges13 bronze badges
1
str_replace is removing com from command to, this not helping
– Matei Zoc
Mar 27 at 10:16
add a comment |
1
str_replace is removing com from command to, this not helping
– Matei Zoc
Mar 27 at 10:16
1
1
str_replace is removing com from command to, this not helping
– Matei Zoc
Mar 27 at 10:16
str_replace is removing com from command to, this not helping
– Matei Zoc
Mar 27 at 10:16
add a comment |
3 Answers
3
active
oldest
votes
You need to use preg_replace. Basically preg_replace() searches the subject for pattern matches and replaces them with the replacement.
<?php
$string = 'Hello world, command data test com';
$string = preg_replace('/bcomb/', '', $string);
echo $string;
?>
Explanation: Above example pattern is explained below
b: Match a word boundary.
com: Text to match.
For more Special Character Definitions check this
Thank you! this is what i was need!
– Matei Zoc
Mar 27 at 10:25
You're welcome!
– Shanteshwar Inde
Mar 27 at 10:25
This can easily done bystr_replace().
– Smartpal
Mar 27 at 10:26
@Smartpal i don't think so, str_replace() replace command to mand
– Shanteshwar Inde
Mar 27 at 10:27
@ShanteshwarInde Yeah that's.... Sorry i forgot com in command. Up Voted
– Smartpal
Mar 27 at 10:32
|
show 1 more comment
Short version:
join(' ',array_diff(explode(' ', $string), ['com']));
Explanation:
explode(' ', $string)splits your string into an array of wordsarray_diff($words, ['com'])removes the elements on the second array from the first array. So in case the array of$wordscontains the wordcom, it will be removedjoin(' ', $words)concats all the strings in the$wordsarray, dividing each word from each other with a space.
Full snippet:
<?php
$string = 'Hello world, command data test com';
$words = explode(' ', $string);
$words = array_diff($words, ['com']);
$string = join(' ', $words);
even though my answer has more upvotes, but this one is my favourite! just add explanation so more people understand. Thanks!
– Shanteshwar Inde
Mar 27 at 10:36
I honestly think your one is better! Ahahah I will add some better explaination
– gbalduzzi
Mar 27 at 10:41
add a comment |
Another version....
If your input string alway in this order You can do it with rtrim
Snippet
$string = 'Hello world, command data test com';
$string = rtrim($string, ' com');
echo $string;
Output
Hello world, command data test
Live demo
Docs
rtrim
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55374717%2fhow-to-remove-exact-match-from-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to use preg_replace. Basically preg_replace() searches the subject for pattern matches and replaces them with the replacement.
<?php
$string = 'Hello world, command data test com';
$string = preg_replace('/bcomb/', '', $string);
echo $string;
?>
Explanation: Above example pattern is explained below
b: Match a word boundary.
com: Text to match.
For more Special Character Definitions check this
Thank you! this is what i was need!
– Matei Zoc
Mar 27 at 10:25
You're welcome!
– Shanteshwar Inde
Mar 27 at 10:25
This can easily done bystr_replace().
– Smartpal
Mar 27 at 10:26
@Smartpal i don't think so, str_replace() replace command to mand
– Shanteshwar Inde
Mar 27 at 10:27
@ShanteshwarInde Yeah that's.... Sorry i forgot com in command. Up Voted
– Smartpal
Mar 27 at 10:32
|
show 1 more comment
You need to use preg_replace. Basically preg_replace() searches the subject for pattern matches and replaces them with the replacement.
<?php
$string = 'Hello world, command data test com';
$string = preg_replace('/bcomb/', '', $string);
echo $string;
?>
Explanation: Above example pattern is explained below
b: Match a word boundary.
com: Text to match.
For more Special Character Definitions check this
Thank you! this is what i was need!
– Matei Zoc
Mar 27 at 10:25
You're welcome!
– Shanteshwar Inde
Mar 27 at 10:25
This can easily done bystr_replace().
– Smartpal
Mar 27 at 10:26
@Smartpal i don't think so, str_replace() replace command to mand
– Shanteshwar Inde
Mar 27 at 10:27
@ShanteshwarInde Yeah that's.... Sorry i forgot com in command. Up Voted
– Smartpal
Mar 27 at 10:32
|
show 1 more comment
You need to use preg_replace. Basically preg_replace() searches the subject for pattern matches and replaces them with the replacement.
<?php
$string = 'Hello world, command data test com';
$string = preg_replace('/bcomb/', '', $string);
echo $string;
?>
Explanation: Above example pattern is explained below
b: Match a word boundary.
com: Text to match.
For more Special Character Definitions check this
You need to use preg_replace. Basically preg_replace() searches the subject for pattern matches and replaces them with the replacement.
<?php
$string = 'Hello world, command data test com';
$string = preg_replace('/bcomb/', '', $string);
echo $string;
?>
Explanation: Above example pattern is explained below
b: Match a word boundary.
com: Text to match.
For more Special Character Definitions check this
edited Jul 12 at 4:53
answered Mar 27 at 10:22
Shanteshwar IndeShanteshwar Inde
1,1742 gold badges11 silver badges20 bronze badges
1,1742 gold badges11 silver badges20 bronze badges
Thank you! this is what i was need!
– Matei Zoc
Mar 27 at 10:25
You're welcome!
– Shanteshwar Inde
Mar 27 at 10:25
This can easily done bystr_replace().
– Smartpal
Mar 27 at 10:26
@Smartpal i don't think so, str_replace() replace command to mand
– Shanteshwar Inde
Mar 27 at 10:27
@ShanteshwarInde Yeah that's.... Sorry i forgot com in command. Up Voted
– Smartpal
Mar 27 at 10:32
|
show 1 more comment
Thank you! this is what i was need!
– Matei Zoc
Mar 27 at 10:25
You're welcome!
– Shanteshwar Inde
Mar 27 at 10:25
This can easily done bystr_replace().
– Smartpal
Mar 27 at 10:26
@Smartpal i don't think so, str_replace() replace command to mand
– Shanteshwar Inde
Mar 27 at 10:27
@ShanteshwarInde Yeah that's.... Sorry i forgot com in command. Up Voted
– Smartpal
Mar 27 at 10:32
Thank you! this is what i was need!
– Matei Zoc
Mar 27 at 10:25
Thank you! this is what i was need!
– Matei Zoc
Mar 27 at 10:25
You're welcome!
– Shanteshwar Inde
Mar 27 at 10:25
You're welcome!
– Shanteshwar Inde
Mar 27 at 10:25
This can easily done by
str_replace() .– Smartpal
Mar 27 at 10:26
This can easily done by
str_replace() .– Smartpal
Mar 27 at 10:26
@Smartpal i don't think so, str_replace() replace command to mand
– Shanteshwar Inde
Mar 27 at 10:27
@Smartpal i don't think so, str_replace() replace command to mand
– Shanteshwar Inde
Mar 27 at 10:27
@ShanteshwarInde Yeah that's.... Sorry i forgot com in command. Up Voted
– Smartpal
Mar 27 at 10:32
@ShanteshwarInde Yeah that's.... Sorry i forgot com in command. Up Voted
– Smartpal
Mar 27 at 10:32
|
show 1 more comment
Short version:
join(' ',array_diff(explode(' ', $string), ['com']));
Explanation:
explode(' ', $string)splits your string into an array of wordsarray_diff($words, ['com'])removes the elements on the second array from the first array. So in case the array of$wordscontains the wordcom, it will be removedjoin(' ', $words)concats all the strings in the$wordsarray, dividing each word from each other with a space.
Full snippet:
<?php
$string = 'Hello world, command data test com';
$words = explode(' ', $string);
$words = array_diff($words, ['com']);
$string = join(' ', $words);
even though my answer has more upvotes, but this one is my favourite! just add explanation so more people understand. Thanks!
– Shanteshwar Inde
Mar 27 at 10:36
I honestly think your one is better! Ahahah I will add some better explaination
– gbalduzzi
Mar 27 at 10:41
add a comment |
Short version:
join(' ',array_diff(explode(' ', $string), ['com']));
Explanation:
explode(' ', $string)splits your string into an array of wordsarray_diff($words, ['com'])removes the elements on the second array from the first array. So in case the array of$wordscontains the wordcom, it will be removedjoin(' ', $words)concats all the strings in the$wordsarray, dividing each word from each other with a space.
Full snippet:
<?php
$string = 'Hello world, command data test com';
$words = explode(' ', $string);
$words = array_diff($words, ['com']);
$string = join(' ', $words);
even though my answer has more upvotes, but this one is my favourite! just add explanation so more people understand. Thanks!
– Shanteshwar Inde
Mar 27 at 10:36
I honestly think your one is better! Ahahah I will add some better explaination
– gbalduzzi
Mar 27 at 10:41
add a comment |
Short version:
join(' ',array_diff(explode(' ', $string), ['com']));
Explanation:
explode(' ', $string)splits your string into an array of wordsarray_diff($words, ['com'])removes the elements on the second array from the first array. So in case the array of$wordscontains the wordcom, it will be removedjoin(' ', $words)concats all the strings in the$wordsarray, dividing each word from each other with a space.
Full snippet:
<?php
$string = 'Hello world, command data test com';
$words = explode(' ', $string);
$words = array_diff($words, ['com']);
$string = join(' ', $words);
Short version:
join(' ',array_diff(explode(' ', $string), ['com']));
Explanation:
explode(' ', $string)splits your string into an array of wordsarray_diff($words, ['com'])removes the elements on the second array from the first array. So in case the array of$wordscontains the wordcom, it will be removedjoin(' ', $words)concats all the strings in the$wordsarray, dividing each word from each other with a space.
Full snippet:
<?php
$string = 'Hello world, command data test com';
$words = explode(' ', $string);
$words = array_diff($words, ['com']);
$string = join(' ', $words);
edited Mar 27 at 10:46
answered Mar 27 at 10:23
gbalduzzigbalduzzi
2,7718 silver badges26 bronze badges
2,7718 silver badges26 bronze badges
even though my answer has more upvotes, but this one is my favourite! just add explanation so more people understand. Thanks!
– Shanteshwar Inde
Mar 27 at 10:36
I honestly think your one is better! Ahahah I will add some better explaination
– gbalduzzi
Mar 27 at 10:41
add a comment |
even though my answer has more upvotes, but this one is my favourite! just add explanation so more people understand. Thanks!
– Shanteshwar Inde
Mar 27 at 10:36
I honestly think your one is better! Ahahah I will add some better explaination
– gbalduzzi
Mar 27 at 10:41
even though my answer has more upvotes, but this one is my favourite! just add explanation so more people understand. Thanks!
– Shanteshwar Inde
Mar 27 at 10:36
even though my answer has more upvotes, but this one is my favourite! just add explanation so more people understand. Thanks!
– Shanteshwar Inde
Mar 27 at 10:36
I honestly think your one is better! Ahahah I will add some better explaination
– gbalduzzi
Mar 27 at 10:41
I honestly think your one is better! Ahahah I will add some better explaination
– gbalduzzi
Mar 27 at 10:41
add a comment |
Another version....
If your input string alway in this order You can do it with rtrim
Snippet
$string = 'Hello world, command data test com';
$string = rtrim($string, ' com');
echo $string;
Output
Hello world, command data test
Live demo
Docs
rtrim
add a comment |
Another version....
If your input string alway in this order You can do it with rtrim
Snippet
$string = 'Hello world, command data test com';
$string = rtrim($string, ' com');
echo $string;
Output
Hello world, command data test
Live demo
Docs
rtrim
add a comment |
Another version....
If your input string alway in this order You can do it with rtrim
Snippet
$string = 'Hello world, command data test com';
$string = rtrim($string, ' com');
echo $string;
Output
Hello world, command data test
Live demo
Docs
rtrim
Another version....
If your input string alway in this order You can do it with rtrim
Snippet
$string = 'Hello world, command data test com';
$string = rtrim($string, ' com');
echo $string;
Output
Hello world, command data test
Live demo
Docs
rtrim
answered Mar 27 at 10:40
SmartpalSmartpal
1,0801 gold badge6 silver badges17 bronze badges
1,0801 gold badge6 silver badges17 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55374717%2fhow-to-remove-exact-match-from-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
str_replace is removing com from command to, this not helping
– Matei Zoc
Mar 27 at 10:16