0
$text=" ma mee ma mee meena mia meenaùs";

$text is a string with hundreds of words,above is a short illustration.

what i want is i want to check if the $text contains 'mee' if yes than replace mee with " "(i.e. space).

i am using preg_replace for the same but as obvious it searches for the pattern mee and replaces it with space even when the $text has meena.

in short i want the replacement only when the $text value is mee not meena.how to achieve this using PREG_REPLACE or related function? Thanks in advance

$text = preg_replace("mee","", $text);

expected output: "ma ma meena mia meenaùs" there seem to some encoding issue as it is not working for meenaùs

3 Answers3

3

As a regular expression it would be ^mee$ which you would use with preg_replace like so:

$text = preg_replace("/^mee$/","", $text);

^ matches the start of the string
$ matches the end of the string

This is quite a simple replacement to do however so it may be more efficient to do it without using regular expressions.


Ok seeing as you've changed the question the answer for the new question would be:

$text = preg_replace("/\bmee\b/","", $text);

This should match the pattern mee where it is a word on it's own and not part of another word.

AeroX
  • 3,387
  • 2
  • 25
  • 39
  • what if $text is a string with hundred of words. –  Dec 18 '13 at 11:18
  • @user2935196 well that requires a different answer. The regular expression for that would be `\bmee\b` as `\b` matches a word boundary. – AeroX Dec 18 '13 at 11:27
2
if ($text == "mee")
    $text = " ";

That's it.

If you want the word "mee" changed in a string that contains this word many times, use $text = preg_replace('/\bmee\b/', ' ', $text);.

Thomas Ruiz
  • 3,611
  • 2
  • 20
  • 33
  • this is not what i want ,i want to perform this using preg_replace or related functions –  Dec 18 '13 at 11:09
  • Please see my comment under your question – Martijn Dec 18 '13 at 11:09
  • 1
    @user2935196 preg_replace will be like 1000x slower than this. Still AeroX has answered what you want (which, imo, is not the right answer) – Thomas Ruiz Dec 18 '13 at 11:13
  • @user2935196 Regex is not really the right tool for this job. The simple compare and replace example given by Thomas is far more efficient. – AeroX Dec 18 '13 at 11:15
  • what if $text is a string with hundreds of words. –  Dec 18 '13 at 11:18
  • @user2935196 you mean `"ma mee ma mee mia"` should be replaced with `"ma ma mia"` ? – Thomas Ruiz Dec 18 '13 at 11:22
  • @ThomasRuiz ya right ma mee ma mee meena mia should be "ma ma meena mia" –  Dec 18 '13 at 11:25
  • @user2935196 check out this question : http://stackoverflow.com/questions/3426265/php-string-replace-match-whole-word?rq=1 – Thomas Ruiz Dec 18 '13 at 11:26
0

You may go for a combination of preg_match and preg_replace.

Check the string for the exact matched text first:

    <?php
    $text=" ma mee ma mee meena mia";
    echo $text."<br/>";
    $key = "mee";
    $rep_text = "YES";

    $text = preg_replace("/\b".$key."\b/u", $rep_text, $text);
    echo $text."<br/>";

    ?>

Try this.

Language Lassi
  • 2,520
  • 21
  • 24