How to get one parameter from a list of elements in php

I have one field in a database table with the following rows

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

All content is stored as text in a single mysql table field. Now I want to get menu_image. How can i get this?

+3
source share
3 answers

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

$str = fetch_from_db();
$array = parse_ini_string($str);
echo $array['menu_image']; // hd_events.png
+1
source

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';
0
source

, Artefacto. - , un-joomla-ify :

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
0

Source: https://habr.com/ru/post/1748783/


All Articles