Jump to content

[SOLVED] storing and retrieving strings with variables


anatak

Recommended Posts

Does anybody know how to store a string with a variable in a database and then retrieving it ?

 

for example

I would like to store a string like

Welcome $username

and when it is retrieved I want to display

Welcome David (if David is the value for $username)

 

Is this possible ?

 

I store the string in a text field in mysql

Maybe my first post was not clear.

 

how can I format the string to be stored in the database so that when I retrieve it the variables in that string are parsed (??)

 

I know how to connect to a db and select data

 

so I have a table with messages in different languages.

Table message

ID 1

Lan1 Welcome $username

Lan2 Bienvenue $username

 

ID2

Lan1 See you $username

Lan2 Au revoir $username

 

now I want to parse the $username when I print the string from the database

$result=$dbconnection($Query)
foreach($row as $result){
echo $row[Lan1]
}

what this prints now is

Welcome $username

See you $username

 

Say that the $username is David

what I would like is to print

Welcome David

See you David

 

So to recap

How can I format a string so that when I retrieve it from the DB the variables are parsed.

 

sorry English != native language

Thanks Gareth,

 

you actually gave me an idea

maybe I can do something different.

 

Is it possible to select a substring on a start and finish character ? Here I would like to select the variable with start character $ and end character space.

 

aaaargghhh

regular expressions  :confused:

 

 

This probably isn't the best way of doing it, but this is the best way I know

 

<?php

$String = '$a$b $c';
$a = "Testing ";
$b = "Testing b";
$c = "Testing c";

preg_match_all('~\$([^\s])~',$String,$Matches);

foreach($Matches[1] as $k=>$v)
{
$Search[] = '~\$' . $v . '~';
$Replace[] = $$v;
}

$String = preg_replace($Search, $Replace, $String);
echo $String;

?>

 

I use regex instead of str_replace because at the moment I'm trying to become more proficient with regex. It's incredibly useful... and it makes me feel smart XD

 

Anatak, you should learn regex, it's really, really awesome. Here's a good starting place

 

http://www.regular-expressions.info/

Hi corbin, whad up world

 

Yeah I know that regex is super usefull

http://xkcd.com/208/

 

I just never got around to learn it but I ll have to I guess.

Just got another idea to work around this problem but I think your solution is more elegant

will try it out after work.

 

wish I could give up my teaching job and become a full time programming nerd :)

Hey Gareth,

 

your code is exactly what I was looking for but I have a (small) problem

the regex only looks for the first letter after the $ sign, could you modify it to select the full variable ?

 

I am trying to replace $language in a string but it only searches for and replaces $l instead of $language

Is it possible to define with regex a non roman character (say Japanese or Chinese) ?

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.