str_replace with assoc multi arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paolodg
Forum Newbie
Posts: 5
Joined: Thu Nov 02, 2006 4:40 am

str_replace with assoc multi arrays

Post by paolodg »

How is this done with assoc multidimetional arrays??

eg:

url: ?page=welcome

Code: Select all

<?php

$page = $_GET['page'];

// array holding text to be replaced
$text['welcome']['{section.bla}'] = "bla bla bla";
$text['welcome']['{section.foo}'] = "foo foo foo";
$text['welcome']['{section.bar}'] = "bar bar bar";

$string = "{section.bla}{section.foo}{section.bar}";

//not correct!!! but how then???
print str_replace($text[$page][?], $text[$page][?][], $string);

?>
QUESTION how do you model this str_replace???


in practice $string is an html template 'got' with file_get_contents()
and the array is used to hold diff. language versions of the same site.

so i want it to search the value of the second dimention assoc_key and replace it with its contents to $string.

i hope this makes sence.

Many thanks in advance,

Paolo

PS: I have tried this with foreach() and for() loops, but to no avail, on an entire document (file_get_contents()) and on line by line (fopen(),fgets(),feof(),fclose())
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

print str_replace(array_keys($text[$page]), array_values($text[$page]), $string);
paolodg
Forum Newbie
Posts: 5
Joined: Thu Nov 02, 2006 4:40 am

Post by paolodg »

LOL

:D

sometimes something is biting you in the butt it's so obvious but u'r blind to it!!

thank you very much...

Beers are on me next time i'm in Berlin. (Live in Switzerland)
Last edited by paolodg on Thu Nov 02, 2006 5:10 am, edited 1 time in total.
paolodg
Forum Newbie
Posts: 5
Joined: Thu Nov 02, 2006 4:40 am

Post by paolodg »

btw, would my question be classified as a 'smart' question??

probably not, lol :lol:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

paolodg wrote:btw, would my question be classified as a 'smart' question??
I believe it's a pretty good one.
  • meaningful subject
  • compact, ready-to-test code snippet that's free of unrelated stuff.
  • description's not using all the wrong terms
  • short notice of what you've tried so far
yeah, pretty good ;)
paolodg
Forum Newbie
Posts: 5
Joined: Thu Nov 02, 2006 4:40 am

Post by paolodg »

well, that pleases me at least, asking a dumb question in a good way :lol:
Thanks very much again, you deserve the title of guru :)
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

paolodg wrote:btw, would my question be classified as a 'smart' question??

probably not, lol :lol:
believe it or not, your question actually solves a New Problem I have been facing!
paolodg
Forum Newbie
Posts: 5
Joined: Thu Nov 02, 2006 4:40 am

Post by paolodg »

blacksnday wrote:
paolodg wrote:btw, would my question be classified as a 'smart' question??

probably not, lol :lol:
believe it or not, your question actually solves a New Problem I have been facing!
Happy to help... :P :lol:
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

The problem I had been facing, until this post was:

I have been developing a Breadcrumb navigation system based upon available pre-defined actions
combined with open $_GET requests.

As you can imagine, this kinda system is ripe for abuse unless you can tell
your breadcrumb system what it should expect, and anything other then expectation
should be denied and not shown....

Thanks for your question :P
Post Reply