-2

I want to replace what is between the }string{ with }string%{

I'm adding the the % to edit a search query.

string example - {a26f089ed1530be8648027b493a21bfd44c5fb15d632afc8f1c539c7c3da88a4}out{a26f089ed1530be8648027b493a21bfd44c5fb15d632afc8f1c539c7c3da88a4}

The 2 values between the pair of curlies changes each time {rAndows2ring}

I've tried a lot of variations and closest I've got to is;

$where = preg_replace("/\}([^}]+)\{/", "}$1%{", $where);

But that returns an extra % at the first {.

%{212190298d692385253efb2a1062006ddf3ea008e3cdc2f8b8f11884ec863202}out%{212190298d692385253efb2a1062006ddf3ea008e3cdc2f8b8f11884ec863202}

thanks in advance!

v3nt
  • 2,638
  • 6
  • 32
  • 49

1 Answers1

-1

This works:

<?php

$oldString = '{a26f089ed1530be8648027b493a21bfd44c5fb15d632afc8f1c539c7c3da88a4}out{a26f089ed1530be8648027b493a21bfd44c5fb15d632afc8f1c539c7c3da88a4}';
echo '<p>oldString: ';
var_dump($oldString);
echo '</p>';


$newString = preg_replace('#(\{[\d\w]+\})([\d\w]+)(\{[\d\w]+\})#', '$1$2%$3', $oldString);

echo '<p>newString: ';
var_dump($newString);
echo '</p>';

?>
  • (\{[\d\w]+\}) captures first string of digits and word characters enclosed with curly brackets.
  • ([\d\w]+) captures 'out', or any other string of digits and word characters in between
  • (\{[\d\w]+\}) same as above
Adam Winter
  • 973
  • 7
  • 19