Welcome Guest, Not a member yet? Register   Sign In
string parsing in between curly bracket
#1

[eluser]Reynolds[/eluser]
Hi guys,

can you tell me how do this?

Code:
<?php
$str = "hello{key:value}world";
// from this string, I would like to find and replace the {key:value}, let's say for example:

$str_2 = "The item {item_name:123} costs {item_amount:123}."; // item_id = 123

will give a result of

$str_2 = "The item Samsung Galaxy S costs $999.";
#2

[eluser]Nick_MyShuitings[/eluser]
I'm not sure if your question is clear enough to help. Is that in a view,controller etc? Are you using a templating engine?

This php function might be what you're looking for: http://php.net/manual/en/function.sprintf.php

Code:
<?php
$format = 'The %2$s contains %1$d monkeys.
           Thats a nice %2$s full of %1$d monkeys.';
printf($format, $num, $location);
?>
#3

[eluser]Reynolds[/eluser]
[quote author="Nick_MyShuitings" date="1294216509"]I'm not sure if your question is clear enough to help. Is that in a view,controller etc? Are you using a templating engine?

This php function might be what you're looking for: http://php.net/manual/en/function.sprintf.php

Code:
<?php
$format = 'The %2$s contains %1$d monkeys.
           That\'s a nice %2$s full of %1$d monkeys.';
printf($format, $num, $location);
?>
[/quote]


my bad, what I meant was...

In mysql, I have table named items

|ITEM_ID|ITEM_NAME|ITEM_PRICE|



In CI controller, I have a method-- I want to replace those that requests an item information, and replace it with whatever in the mysql-table above. This replaced string is then passed to a view.

My thinking is similar to CI's templating, except that it should be just implemented as a helper function, of which I just need to find those codes e.g. "{item_name:123}" and replace it with the item_id 123 from the items mysql-table.

and this function actually captures the entire html string contents, and replace whatever needs to be replaced, then return the result to the output view.

Not sure, if I stated it right, I'm a little dizzy right now, thinking as to how can I pull this :|
#4

[eluser]Nick_MyShuitings[/eluser]
Why does it have to happen in the controller like that? Is there a reason that you haven't explained yet?

If not it seems like you'd be putting an aweful lot of time into this, when you could just as easily do a foreach on the db result.

example:

Controller+Model
Code:
$this->data['items'] = $this->items_model->get_items()->result();
$this->data['item-1'] = $this->items_model->get_item_by_id(1)->row();

View
Code:
<?foreach($items as $item):?>
The item <?=$item->item_name?> costs <?=$item->item_price?>
<?endforeach;?>

OR

The item <?=$item-1->item_name .... etc

I guess I'm just not seeing how what you propose would benefit you at all, and it would be a doosy of a function having to look for your {} tags, and then pass the data inside them into a library function which would need to call the model function and then rewrite the string (ie, lots of work and processing time)
#5

[eluser]Reynolds[/eluser]
Perhaps I should state what's going on.


I have template.html file (containing html tags, with a template variable "{key:value}"), now from the Controller I read that file, and store it into a php variable, and find those template variables in curly bracket, and replace it of whatever I find from the DB. Finally, I pass the the result to a view, and displays it.


---
to narrow it, I just want to get the value inside the curly bracket, and replace it with a new value, knowing that the values inside these curly brackets varies/dynamic.

Code:
$str = "Hello{space}world";

// and make it into
$str = "Hello world";
#6

[eluser]Nick_MyShuitings[/eluser]
k, in that case you need to work out some logic on your key:values pairs. For example: is this only for the items table? do you want the solution to be more flexible for potential later use? etc.

as is you'll need to do the following:

1) preg_replace_callback() on the string for {whatever}
2) call a library function that will parse the finds
3) that function will probably explode them and call a function $key($value)
4) magic
#7

[eluser]Reynolds[/eluser]
Nice,

thanks for the enlightenment Nick Smile

one last thing, I'm not really that good with regex patterns... so... where can I grab a cheat for that? Wink

I mean, I've been trying to search with the same problem I have, and trying to copy and pastes patterns, but they're not working to what exactly what I want for it to do.
Thanks again Smile
#8

[eluser]Nick_MyShuitings[/eluser]
I am also horrible with them, they are the bane of my existence... but google is our friend and showed a few tuts... give this a try:

'#\{[^)]+\}#'

basically says find anything inside of { and }. Should return multiples, make sure to properly code for the case that there are no tags founds.
#9

[eluser]Reynolds[/eluser]
Thanks a lot Nick, it seems to be what I'm looking for Smile
#10

[eluser]umefarooq[/eluser]
nice thread im also looking for code or library which can work like wordpress short codes which can help users to and tags from backend and in front end it become html or your desired output. here is one library which can help you more

https://bitbucket.org/dhorrigan/simpletags/src




Theme © iAndrew 2016 - Forum software by © MyBB