tags:

views:

65

answers:

3

I have one field in database table with following lines

show_email_icon=
show_hits=
feed_summary=
page_title=
show_page_title=1
pageclass_sfx=
menu_image=hd_events.png
secure=0

All the stuff is stored as text in one field of mysql table

Now i want to get the menu_image.

How can i get that

A: 

Try

if (preg_match('/^menu_image\\s*=\\s*(.*)$/m', $t, $matches)) {
    //the content is $matches[1];
}

You should consider having a table with two columns "key" and "value" and then

SELECT value from table_name WHERE key = 'menu_image';
Artefacto
I don't have any control over that. WIll your code work if other variables are populated as well . because they may not be empty everytime
Mirage
@Mirage yes. It matches only the line that starts with "menu_image". It also allows whitespace around the equals sign. You can remove the two `\\s*` portions do disallow that.
Artefacto
A: 

If you just need that one value I'd use Artefacto's solution. If you're going to need any others, it's probably best to un-joomla-ify it and turn that string into a map:

preg_match_all("/(.*)=(.*)/", $dbString, $matches); 
$settings = array();
for($i = 0; $i < count($matches[1]); $i++)
    $settings[$matches[1][$i]] = $matches[2][$i];

// Use $settings['menu_image'] or any of the others
Michael Mrozek
+1  A: 

If you are running PHP5.3 or higher, you can use parse_ini_string() which will give you a nice associative array:

$str = fetch_from_db();
$array = parse_ini_string($str);
echo $array['menu_image']; // hd_events.png
Andrew Moore