Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP Find string between 2 parenthesises

CarlEOgden

Active Coder
Hi

I have a variable (a document) that has variables set within it, for example:-

[CODE lang="php" title="Variable contains"]Dear {VendorSalutation},



Please contact me on {OfficeTelephone} as soon as possible.



Regards



{StaffMember}

{OfficeTelephone}
{OfficeEmail}[/CODE]


Now, I'd like to search this variable and extract all the variables held within {}.

Any quick solutions to this?

Cheers in advance
Carl.
 
I am a little rusty - so this took a little longer then I thought it would.

Code:
<?php
$string="Dear {VendorSalutation},



Please contact me on {OfficeTelephone} as soon as possible.



Regards



{StaffMember}

{OfficeTelephone}
{OfficeEmail}";

$array1 = explode("{", $string);
$array2 = [];


for ($x = 1; $x <= count($array1)-1; $x++) {
  $temp = explode("}", $array1[$x]);
  array_push($array2, $temp[0]);
}

/* The rest of this is to show you what is what */


foreach ($array2 as $content) {
  echo "$content <br>";
}

echo "------------------------------------------<br><br>";
echo '$array2[0] is '.$array2[0]."<br>";
echo '$array2[1] is '.$array2[1]."<br>";
echo '$array2[2] is '.$array2[2]."<br>";
echo '$array2[3] is '.$array2[3]."<br>";
echo '$array2[4] is '.$array2[4];
?>
 
I am a little rusty - so this took a little longer then I thought it would.

Code:
<?php
$string="Dear {VendorSalutation},



Please contact me on {OfficeTelephone} as soon as possible.



Regards



{StaffMember}

{OfficeTelephone}
{OfficeEmail}";

$array1 = explode("{", $string);
$array2 = [];


for ($x = 1; $x <= count($array1)-1; $x++) {
  $temp = explode("}", $array1[$x]);
  array_push($array2, $temp[0]);
}

/* The rest of this is to show you what is what */


foreach ($array2 as $content) {
  echo "$content <br>";
}

echo "------------------------------------------<br><br>";
echo '$array2[0] is '.$array2[0]."<br>";
echo '$array2[1] is '.$array2[1]."<br>";
echo '$array2[2] is '.$array2[2]."<br>";
echo '$array2[3] is '.$array2[3]."<br>";
echo '$array2[4] is '.$array2[4];
?>
That is brilliant, thank you very much, saved me hours of trying to code this.

Cheers
Carl
 

Buy us a coffee!

Back
Top Bottom