0

Searched up and down, but I couldn't find anything that works.
I want to strip everything except you.

$words = "you,us,them,our";
$keep = "you,";

This does the opposite:

$words = str_replace("$keep", "", $words);

How do I strip everything except $keep?

zx485
  • 28,498
  • 28
  • 50
  • 59

1 Answers1

0
str_replace(array('us', 'them', 'our', ','), array('','','',''), $words);

Although this is a little bad, of course. But if you only have some replacements this will do its job. It will become ugly when you want to replace more things. You might want to add a pattern and use preg_replace with the not operator to strip everything except 'you'.

Like so:

$text = "You,I,He";
$matches = preg_replace("/[^you]/Uims", "", $text);

print_r($matches);
evayly
  • 832
  • 7
  • 18
  • the second example works, except it leaves the commas. How do I get read of them? – Vitali Kloster May 07 '18 at 01:19
  • How is this output different from simply running `$words = rtrim($keep,",");` ? – lufc May 07 '18 at 01:53
  • I've got another question. It matches words like `you,me,our` just fine, but when I tried two long codes separated by a comma `(63bcd4cf4316b070ec6382f54c59626d,3e90e2c665f8003e1d84f505df625)`, it failed to pick the right code. It just shows both of them. Any idea why? – Vitali Kloster May 07 '18 at 02:55