Jump to content

Process data in a string.


StrangeWill

Recommended Posts

Okay so I have a string:

"This is a {today.weather} day"

We can replace {today.weather} with $this->$Strings['today']['weather'] then use eval? Right?

Except the string may include "{" or ";" which seems to confuse the hell out of eval.

Example:
'<p style="font-size:28px;">This is a {today.weather} day</p>'

Any ideas on how to overcome this problem efficiently? Wouldn't it be extremely time consuming to loop through every instance of {var.var} (gather them using regex) and then do a replace one by one?
Link to comment
https://forums.phpfreaks.com/topic/32911-process-data-in-a-string/
Share on other sites

[quote]We can replace {today.weather} with $this->$Strings['today']['weather'] then use eval? Right?[/quote]

How? eval doesnt replace anything, it evaluates a string as if it where php. If this...

[code=php:0]
'<p style="font-size:28px;">This is a {today.weather} day</p>'
[/code]

is your string, I don't see what good eval() would be.
[quote author=thorpe link=topic=121057.msg497155#msg497155 date=1167959661]
[quote]We can replace {today.weather} with $this->$Strings['today']['weather'] then use eval? Right?[/quote]

How? eval doesnt replace anything, it evaluates a string as if it where php. If this...

[code=php:0]
'<p style="font-size:28px;">This is a {today.weather} day</p>'
[/code]

is your string, I don't see what good eval() would be.
[/quote]
Well what I'd do is replace {today.weather} with $this->Strings['today']['weather'] than eval that, but that doesn't work.


[quote author=Barand link=topic=121057.msg497167#msg497167 date=1167960748]
Have a look at
http://www.php.net/str_replace
[/quote]
Okay, so what you're saying is I should use regex to get all occurrences of {val.val} in my string, then build that into an array, and do a replace?

I would think that to be EXTREMELY slow, but if that wouldn't be the case, then thanks. :)

So like:
Regex->Get all "{(*.).(*.)}"
Split all (if needed) to get the two separate vals
do(while there are values to process)
{
replace($original regex val, $this->$Strings[$RegexSplit[0],$RegexSplit[1]]);
}

??


Basically I'm building a small (but QUICK) template engine, thats why I'm trying to keep load times small, and need it to process lots of info.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.