Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to find a string within a string ("foo", for example) and then replace it with a PHP code. Is it possible?

I'm creating a Joomla Plugin. When my article has something like "{foo}", i will replace it with an include (require, or whatever).

I've made something like:

public function onContentBeforeDisplay($context, &$row, &$params, $page=0)
    {
        // $row->text has the article text ...
        if (preg_match('/{contact-form}/', $row->text))
        {
            require_once(dirname(__FILE__) . DS . 'tmpl' . DS . 'default.php');
        }
    }

But this code will insert default.php at the beginning. I want to replace it in {contact-form} tag.

Thank you.

share|improve this question
2  
Could you please explain the context of your problem and provide an example? –  Felix Kling Feb 10 '11 at 13:23
 
Have you tried str_replace? –  deceze Feb 10 '11 at 13:24
 
@deceze the question was tagged with str-replace, you'd almost assume the OP checked the docs first... wouldn't you? ;-) –  richsage Feb 10 '11 at 13:26
 
str_replace adds the code without evalute it. –  thom Feb 10 '11 at 13:28
 
@rich Good point. :o) @thom Why don't you run the code first and replace {foo} with the result of the run code? –  deceze Feb 10 '11 at 13:30
show 4 more comments

3 Answers

You can use the PHP function "substr_replace()", whose details are given here. \n";

/* These two examples replace all of $var with 'bob'. */
echo substr_replace($var, 'bob', 0) . "<br />\n";
echo substr_replace($var, 'bob', 0, strlen($var)) . "<br />\n";

/* Insert 'bob' right at the beginning of $var. */
echo substr_replace($var, 'bob', 0, 0) . "<br />\n";

/**
 * Your food & search is here
 */
/* These next two replace 'MNRPQR' in $var with 'bob'. */
echo substr_replace($var, 'bob', 10, -1) . "<br />\n";
echo substr_replace($var, 'bob', -7, -1) . "<br />\n";

/* Delete 'MNRPQR' from $var. */
echo substr_replace($var, '', 10, -1) . "<br />\n";
?>
share|improve this answer

You can create a PHP file that matches the keyword (like: foo.php) and simply extract the keyword from the text and use it to include that file.

For example:

<?php
$str = "This is {foo} text";
$includePath = "/path/to/my/files/";

// Careful with the Regular Expression. If you allow chars like . in the
// keyword this could create a security problem. (Dots allow you to go backwards
// which would allow people to execute files outside your path.)
if (preg_match_all('/\{([\w]+)\}/', $str, $matches))
{
    foreach ($matches[1] as $_match)
    {
        $filePath = $includePath . $_match . ".php";
        if (file_exists($filePath))
        {
            include $filePath;
        }
        else
        {
            trigger_error("File does not exist '$filePath'.", E_USER_WARNING);
        }
    }
}
?>
share|improve this answer

I think you just want this, don't you?

<?php

$replacement = file_get_contents('myfile.php');

$content = str_replace('{foo}', eval($replacement), $content);

?>
share|improve this answer
 
No, I don't want it. –  thom Feb 10 '11 at 14:19
1  
er... okay, fair enough.... :S –  simon Feb 10 '11 at 14:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.