I'm trying to get a string with #hashtags and convert them into array keys! For example:
string = "hello #world";
I want to replace to "hello $line['world']";
I did this:
$query = mysql_query($sql);
while($line = mysql_fetch_assoc($query)) {
$code = preg_replace("/(#(\w+))/", $line['$1'], $string);
echo $code;
}
But I get this warning: "Undefined index: $1" and obviously the echo prints only "hello "
But if I put a valid $line key directly it show its content. Like this:
$query = mysql_query($sql);
while($line = mysql_fetch_assoc($query)) {
$code = preg_replace("/(#(\w+))/", $line[name], $string);
echo $code;
}
It shows me "hello nameFromDatabase" for each line of database...
How can I set this $line[XXX] on the preg_replace to get the name located at the #hashtag replacement?