anatak Posted September 9, 2009 Share Posted September 9, 2009 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 Link to comment https://forums.phpfreaks.com/topic/173619-solved-storing-and-retrieving-strings-with-variables/ Share on other sites More sharing options...
Garethp Posted September 9, 2009 Share Posted September 9, 2009 http://www.w3schools.com/PHP/php_mysql_intro.asp Most people don't like w3schools, but it was my starting point for PHP, and it never failed me Link to comment https://forums.phpfreaks.com/topic/173619-solved-storing-and-retrieving-strings-with-variables/#findComment-915166 Share on other sites More sharing options...
anatak Posted September 9, 2009 Author Share Posted September 9, 2009 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 Link to comment https://forums.phpfreaks.com/topic/173619-solved-storing-and-retrieving-strings-with-variables/#findComment-915171 Share on other sites More sharing options...
Garethp Posted September 9, 2009 Share Posted September 9, 2009 $String = $row['Lan1']; $String = preg_replace('~\$username~', $username, $String); echo $String; I don't know how to do what you're after, so try that Link to comment https://forums.phpfreaks.com/topic/173619-solved-storing-and-retrieving-strings-with-variables/#findComment-915181 Share on other sites More sharing options...
corbin Posted September 9, 2009 Share Posted September 9, 2009 $String = $row['Lan1']; $String = preg_replace('~\$username~', $username, $String); echo $String; I don't know how to do what you're after, so try that Why not just use str_replace? Link to comment https://forums.phpfreaks.com/topic/173619-solved-storing-and-retrieving-strings-with-variables/#findComment-915182 Share on other sites More sharing options...
anatak Posted September 9, 2009 Author Share Posted September 9, 2009 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 Link to comment https://forums.phpfreaks.com/topic/173619-solved-storing-and-retrieving-strings-with-variables/#findComment-915183 Share on other sites More sharing options...
corbin Posted September 9, 2009 Share Posted September 9, 2009 preg_match('/\$([^ ]+) /', $str, $m); Link to comment https://forums.phpfreaks.com/topic/173619-solved-storing-and-retrieving-strings-with-variables/#findComment-915186 Share on other sites More sharing options...
Garethp Posted September 9, 2009 Share Posted September 9, 2009 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/ Link to comment https://forums.phpfreaks.com/topic/173619-solved-storing-and-retrieving-strings-with-variables/#findComment-915191 Share on other sites More sharing options...
anatak Posted September 9, 2009 Author Share Posted September 9, 2009 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 Link to comment https://forums.phpfreaks.com/topic/173619-solved-storing-and-retrieving-strings-with-variables/#findComment-915198 Share on other sites More sharing options...
anatak Posted September 9, 2009 Author Share Posted September 9, 2009 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) ? Link to comment https://forums.phpfreaks.com/topic/173619-solved-storing-and-retrieving-strings-with-variables/#findComment-915225 Share on other sites More sharing options...
Garethp Posted September 9, 2009 Share Posted September 9, 2009 preg_match_all('~\$([^\s]+)~',$String,$Matches); There Link to comment https://forums.phpfreaks.com/topic/173619-solved-storing-and-retrieving-strings-with-variables/#findComment-915226 Share on other sites More sharing options...
anatak Posted September 9, 2009 Author Share Posted September 9, 2009 thanks that works. it selects now the . at the end of the sentence but I ll try to figure out how to modify the regex myself thanks again anatak Link to comment https://forums.phpfreaks.com/topic/173619-solved-storing-and-retrieving-strings-with-variables/#findComment-915234 Share on other sites More sharing options...
Garethp Posted September 9, 2009 Share Posted September 9, 2009 Ok, let me show you something that'll help See, [^\s] that? It means, select any character that's not whitespace. So, to make it select any character that's not whitespace or dots, just change it to [^\s.] Link to comment https://forums.phpfreaks.com/topic/173619-solved-storing-and-retrieving-strings-with-variables/#findComment-915245 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.