preg_match same last 3 digit of a string

56 views Asked by At

Guys I'm kinda dumb in making formula so please can you help me out?

I got this strings

string 'tfa_517' (length=7)
string 'tfa_528' (length=7)
string 'tfa_528' (length=7)
string 'tfa_529' (length=7)
string 'tfa_529' (length=7)

Can you help me combine the two 'tfa_528' and the tfa_529 and leave the one with no pair alone? Thanks

1

There are 1 answers

0
chris85 On

You don't need a regex for this.

  1. `Explode` it on new lines so that each line is a separate value in an array.
  2. Use `array_unique` to remove all duplicates.
  3. `Implode` it back with the "glue" used in the `explode` to have it back as a string.
<?php
$string = "string 'tfa_517' (length=7)
string 'tfa_528' (length=7)
string 'tfa_528' (length=7)
string 'tfa_529' (length=7)
string 'tfa_529' (length=7)";
$newstring = implode("\n", array_unique(explode("\n", $string)));
echo $newstring;

Output:

string 'tfa_517' (length=7)
string 'tfa_528' (length=7)
string 'tfa_529' (length=7)

Function docs:

  1. http://php.net/explode
  2. http://php.net/array_unique
  3. http://php.net/implode