regex - Template string parsing in php -
i having issue string parsing in php. trying create own template parser (for learning purpose). have decided put urls in following format {url:abc/def}
displayed http://www.domain.com/abc/def. have tried using str_replace in case not work since value after ":" anything. exploding string wont work ":" can present in text of html file. think done via regular expression suggest me regular expression let me know if can achieved via other approach.
other things note.
- since used template parsing , template can have multiple urls.
- urls not have query strings appreciate if solution supports them.
for learning purposes then:
preg_replace('/\{url:([^\}]*)\}/', $base_url.'$1', $string);
and explanation of regex
\{url:
<-- find text[^\}]*
<-- means 'any character}
occuring 0 or more times([^\}]*)
<-- putting inbetween()
can later reference (see'$1'
)\}
<-- closing}
as long you're learning: take @ http://www.regular-expressions.info/ more info on regular expressions. they're quite awesome :d
Comments
Post a Comment