views:

32

answers:

2

Hello,

I have a simple text that is being parsed with PHP. In that text, I sometimes use following syntax:

Here's the text... {$video:path/to/my/video.mp4} and here the text goes on.

Now, what I need, is, by means regex, to replace this {$video:path/to/my/video.mp4} with the returned string of this: $someObject->processVideoSource('path/to/my/video.mp4');. So, you see, I need to find these expressions, filter the source from {$video: and }, and then run a function on that source, which replaces the whole expression. How can I do this with preg_replace or stuff like that?

Please Keep in mind that I have different instances of that expression in the text file I am parsing and that each video has its own source. And excuse the badly chosen title ^^

Thanks A LOT in advance!

A: 
preg_replace('/({\$[a-zA-Z]+:)|(})/', '', '{$video:path/to/my/video.mp4}');

So:

$path = preg_replace('/({\$[a-zA-Z]+:)|(})/', '', '{$video:path/to/my/video.mp4}');
$someObject->processVideoSource($path);
Michael Robinson
+2  A: 
preg_replace_callback('/{\\$video:(.+?)}/',
    function ($matches) use ($someObject) {
        return $someObject->processVideoSource($matches[1]);
    }, $text);
Artefacto
SO teaches me so much...
Michael Robinson
+1 but I think you need to escape the $ and possibly the {}
SoapBox
I don't think the "{}" need escaped, just the "$". At least, testing it [here](http://www.spaweditor.com/scripts/regex/index.php) without escaping the "{}" worked fine (but again, must escape the $!).
Stephen
@Soap Thanks, in fact I had to escape `$` for the regex, since it matches the end of the expression otherwise. It's not necessary to escape `{` and `}` in this case. "{ and } are literal characters, unless they're part of a valid regular expression token (e.g. the {n} quantifier)." (http://www.regular-expressions.info/reference.html)
Artefacto