The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. July 22, 2019, 10:38:37 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Can this job be done more easily with REGEX.. it works ok in PHP.. I love regex.  (Read 6099 times)
tommytx
Expert
****
Offline Offline

Posts: 123


View Profile WWW
« 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}

Logged
tommytx
Expert
****
Offline Offline

Posts: 123


View Profile WWW
« Reply #1 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...
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #2 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].

Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!