PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Dec 11, 2018 11:12 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Replacing tags..
PostPosted: Fri Nov 22, 2013 4:51 pm 
Offline
Forum Newbie

Joined: Fri Feb 10, 2012 5:50 pm
Posts: 8
So I am writing something that's like displays text, but uses its replaces certain text with its own random text conditions.

For example:

The string would be: "This '{word}' is a {something} word

where
$var['word'] = 'booze', 'smoke', 'haha'
$var['something'] = 'funny', 'not funny', 'ugly'

So I am wondering what would be the best way to find and replace the words needed with my stored variables?


Top
 Profile  
 
 Post subject: Re: Replacing tags..
PostPosted: Fri Nov 22, 2013 5:40 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
preg_replace_callback() that matches on anything within {}s and looks to $var for a (random) replacement.
Syntax: [ Download ] [ Hide ]
$input = "This '{word}' is a {something} word";
$replacements = array(
        "word" => array("booze", "smoke", "haha"),
        "something" => array("funny", "not funny", "ugly")
);

$output = preg_replace_callback('/\{([^{}]+)\}/', function($matches) use($replacements) {
        if (isset($replacements[$matches[1]])) {
                return $replacements[$matches[1]][array_rand($replacements[$matches[1]])];
        } else {
                // what if the term isn't in the replacements list?
                return $matches[0]; // unchanged
        }
}, $input);


Top
 Profile  
 
 Post subject: Re: Replacing tags..
PostPosted: Fri Nov 22, 2013 11:56 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13583
Location: New York, NY, US
You could also use str_replace()
Syntax: [ Download ] [ Hide ]
$str = "This '{word}' is a {something} word";
$replacements = array(
         "word" => array("booze", "smoke", "haha"),
         "something" => array("funny", "not funny", "ugly"),
         );

$from = array();
$to = array();
foreach ($replacements as $tag => $words) {
         $from[] = '{' . $tag . '}';
         $wordn = rand (0 , count($words)-1 );
         $to[] = $words[$wordn];
}
$str = str_replace($from, $to, $str);
 

_________________
(#10850)


Top
 Profile  
 
 Post subject: Re: Replacing tags..
PostPosted: Sat Nov 23, 2013 1:23 am 
Offline
Forum Newbie

Joined: Fri Feb 10, 2012 5:50 pm
Posts: 8
Here's what I came up with:



Syntax: [ Download ] [ Hide ]
$this->task_msg = preg_replace_callback('/\{([^{}]+)\}/',
$this->function($this->matches) $this->use($this->vars)
{
if (isset($this->vars[$this->matches[1]]))
{
        return $this->vars[$this->matches[1]][$this->vars[$this->vars[$this->matches[1]]]];
}
else
{
// what if the term isn't in the replacements list?
return $this->matches[0]; // unchanged
}
},
$rec['conditionValue']
);     


I am getting the error:

Quote:
Parse error: syntax error, unexpected T_VARIABLE in /home2/founder/public_html/urpg.net/gamefiles/classes/task.php on line 210


Where line 210 is $this->function($this->matches) $this->use($this->vars)

I am using class files, so I added $this-> in front of the function and use because it gave me errors if I didn't. ;-)


Top
 Profile  
 
 Post subject: Re: Replacing tags..
PostPosted: Sat Nov 23, 2013 2:44 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
That's... not... really close to the right syntax. Can't just throw "$this->" in there like that.

Try Chris's solution. It doesn't need crazy things like callbacks and closures.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group