0

My question is, I have a string and I want to remove some elements from it.

$String = "#R1@&0000#R2@&1111#R3@&2222#R4@&3333#R5@&4444#R6@&5555";

Here in this above string I Want to remove this --> #R1 #R2 #R3 #R4 #R5 #R6

So the output should be -->

$String_updated = "@&0000@&1111@&2222@&3333@&4444@&5555";   //#R1..#R2..#Rn has been removed

So any ideas or solution for this task ?

Thank You.

  • Use php str_replace – nacho Nov 18 '20 at 12:07
  • 1
    what research have you done? Where are you stuck? I'm 99% sure this could be answered by studying documentation, tutorials, or previous questions here. I doubt there is much that needs repeating. if you disagree, please show your effort so far and explain where exactly you have a problem. Asking a question here isn't a substitute for making an attempt - ask us _after_ you've tried, not before. If you've already tried, then show us, as I mentioned. String replacement or regex are both solutions which could potentially be used here. – ADyson Nov 18 '20 at 12:13
  • @nacho yes I tried this function but don't know how do I solve this as I tried this function in "for loop" but couldn't completely remove all the elements. I mean outside the loop, I'm not getting the perfect result. – Naman Bhardwaj Nov 18 '20 at 12:13
  • 1
    Ths is a duplicate of [How do I replace certain parts of my string?](https://stackoverflow.com/questions/8163746/how-do-i-replace-certain-parts-of-my-string) (and several others). – ADyson Nov 18 '20 at 12:26
  • Well 1) I've posted a link which already explains it, so not sure how that's "useless", particularly. And my advice to hone your research skills will be good for the future, especially if you're not a pro who lives in code. It will save you time. 2) if you tried things, you didn't show it - we can only say what we see. Judging by your downvote count it's not just my opinion either. We get so many freeloaders here that generally it's a good idea to ensure you explain your prior efforts, because people don't want to be just a free coding service. – ADyson Nov 18 '20 at 12:32
  • Fortunately, other people (presumably not the downvoters) have been kind to you on this occasion with their answers, since it's actually quite a trivial problem (and they can always just copy from the many existing solutions available online, if they want to). – ADyson Nov 18 '20 at 12:33
  • Good guess but no. Actually my profile page already tells you the answer (again...research) – ADyson Nov 18 '20 at 12:41
  • @ADyson by looking at your profile (finally I researched.. lol). Oh Jesus where were you before, I'm inspired , you opened my eyes. xD – Naman Bhardwaj Nov 18 '20 at 12:49
  • @ADyson please don't mind, I respect you my senior. – Naman Bhardwaj Nov 18 '20 at 12:56

3 Answers3

1

You can use this to get your expected output:

echo str_replace(["#R1","#R2","#R3","#R4","#R5","#R6"],"",$String);
PRAMIT PAUL
  • 104
  • 1
  • 9
1

You can use a simple regular expression:

$re = '/(#R\d)/m';
$str = '#R1@&0000#R2@&1111#R3@&2222#R4@&3333#R5@&4444#R6@&5555';
$subst = '';

$result = preg_replace($re, $subst, $str);

echo "The result of the substitution is ".$result;
  • No probs and thanks for the points. Just be aware that this will work only if the R? is a single digit. –  Nov 18 '20 at 12:33
0

The complete solution when number is major 9. e.g.:10

$str = "#R11@&0000#R22@&1111#R3@&2222#R4@&3333#R5@&4444#R6@&5555";
$pattern = "/#R(.*?)@/i";
echo preg_replace($pattern, "@", $str);
//@&0000@&1111@&2222@&3333@&4444@&5555
Darlan Dieterich
  • 2,369
  • 1
  • 27
  • 37