You are here: selecting <option> with preg_replace vs loop - which is better? « PHP Programming Language « IT news, forums, messages
selecting <option> with preg_replace vs loop - which is better?

Posted by zorro on 05/19/06 16:11

and does testing merely mean running it 100 000 times and clocking it?


// using preg_replace :

$s = '
<select name="color">
<option value="">choose
<option value="red">red
<option value="green">green
<option value="blue">blue
<option value="yellow">yellow
</select>';

if($_POST)
{ $c=$_POST['color'];
$s=preg_replace('/(value="'.$c.'")/','$1 selected',$s);
}

echo $s;


// using loop :

$colors=array('red','green','blue','yellow');

echo '<select name="color">';
echo '<option value="">choose';
foreach($colors as $c){
echo '<option value="'.$c.'"';
if($_POST['color']==$c)
echo ' selected';
echo '>'.$c;
}

echo '</select>';

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация