The Cache: Technology Expert's Forum

Level 2 Cache: Speciality Items => Regex => Topic started by: tommytx on September 07, 2011, 01:22:32 PM



Title: Can this job be done more easily with REGEX.. it works ok in PHP.. I love regex.
Post by: tommytx on September 07, 2011, 01:22:32 PM
Is there a simple ReGex for getting the answer in $Find.... I want it to include the {} in the answer..
Actually I need it with and without  but I can strip the {} after I use it with the {}
I am building a WP plugin so I need it with the {} so I can tell it to replace the entire thing in the_content
However once I have that variable saved of course I need to break it apart in to its two separate parts... unless REGEX does that easily I can use PHP to do it... so actually I need the following 3 variables pulled out separate..
Variable 1. {tomurl|http://mydomain.com/newpage.php?dat=test}
Variable 2.  tomurl
Varialbe 3.  http://mydomain.com/newpage.php?dat=test
I know how to do expode and all the junk to separate it unless REGEX can do it in one flail swoop.

Code:
<?php
$text 
= <<<MYDATA
Some stuff.......
{tomurl|http://mydomain.com/newpage.php?dat=test}
More stuff......
MYDATA;
echo 
"$text<br>";

$s1 strpos($text'{');
$s2 strpos($text'}');

echo 
"S1  = $s1<br>";
echo 
"S2  = $s2<br>";

$find substr($text$s1$s2-$s1+1);
echo 
"Find = $find<br>";
?>



Final Results:
TEXT = Some stuff....... {tomurl|http://mydomain.com/newpage.php?dat=test} More stuff......
S1 = 18
S2 = 66
Find = {tomurl|http://mydomain.com/newpage.php?dat=test}



Title: Re: Can this job be done more easily with REGEX.. it works ok in PHP.. I love regex.
Post by: tommytx on September 07, 2011, 01:26:03 PM
Important note... everything inside the {} can change so none of that can be used for selection...
The only thing that will not change is the left and right {}... and there will never be another {} in the text...


Title: Re: Can this job be done more easily with REGEX.. it works ok in PHP.. I love regex.
Post by: perkiset on September 07, 2011, 03:11:24 PM
$buff = 'stuff stuff {inner Stuff and more inner stuff} and outside stuff';
preg_match('/\{(.*?)\}/', $buff, $parts);
echo $parts[1];

Didn't check it but that ought to do it. Grab everything starting after the literal { (non-greedy) until the literal } and collect it into the $parts array. The entire mess will be $parts[0] and the answer you want is in $parts[1].