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.

  1. since used template parsing , template can have multiple urls.
  2. 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

  1. \{url: <-- find text
  2. [^\}]* <-- means 'any character } occuring 0 or more times
  3. ([^\}]*) <-- putting inbetween () can later reference (see '$1')
  4. \} <-- closing }

as long you're learning: take @ http://www.regular-expressions.info/ more info on regular expressions. they're quite awesome :d


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -