Page 1 of 1

str_replace with assoc multi arrays

Posted: Thu Nov 02, 2006 4:51 am
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())

Posted: Thu Nov 02, 2006 4:56 am
by volka

Code: Select all

print str_replace(array_keys($text[$page]), array_values($text[$page]), $string);

Posted: Thu Nov 02, 2006 4:59 am
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)

Posted: Thu Nov 02, 2006 5:07 am
by paolodg
btw, would my question be classified as a 'smart' question??

probably not, lol :lol:

Posted: Thu Nov 02, 2006 5:15 am
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 ;)

Posted: Thu Nov 02, 2006 5:23 am
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 :)

Posted: Thu Nov 02, 2006 5:29 am
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!

Posted: Thu Nov 02, 2006 5:32 am
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:

Posted: Thu Nov 02, 2006 5:37 am
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