tags:

views:

81

answers:

3

if i have a line like this.

<option value="someval">somval</option>

how can i put the position the cursor after the last quotation of value and put something like abcdef ?

so the output would be

<option value="somval" abcdef>somval</option>

with php?

i want to do this dynamically i cant figure out how to do it , im looking at strpos() but dont see how it can be done. what ill be doing with this is ill post a bunch of option tags into a textbox and code will be generated. so ill have alot of options fields.

@martin - say i have a huge dropdown and each option lists a country that exists. rather than having to manually type out something like this:

$query = $db->query("my query....");
while($row = $db->fetch($query)) {

<select name="thename">
<option value="someval" <?php if($row['someval'] == 'someval') { print "selected"; } ?> >someval</option>

<option value="someval" <?php if($row['someval'] == 'someval') { print "selected"; } ?> >someval</option>

<option value="someval" <?php if($row['someval'] == 'someval') { print "selected"; } ?> >someval</option>

... followed by 100 more because there are alot of locations to list.

im just trying to figure out how i can post all the options i have into a textbox and have the above code automatically generated to save alot of time. get what i mean?

A: 

You could capture (value=".+?") and replace it with $0 abcdef.

<?php

  $string = '<option value="someval">someval</option>';
  print preg_replace("/(value=\".+?\")/i", "$0 abcdef", $string);

?>

Which outputs the following:

<option value="someval" abcdef>someval</option>
Jonathan Sampson
A: 

with PHP you can generate whole string with any text you wish. where do you have your original string? in a variable or a text file?

Col. Shrapnel
+2  A: 

Using your example you would do:

while($row = $db->fetch($query)) {
    printf('<option value="someval"%s>someval</option>',
            ($row['someval'] == 'someval') ? ' selected="selected" ' : '');
}

This would go through the rows and output an option, replacing the %s with the attribute selected="selected" if $row['someval'] is equal to someval. However, the above is rather pointless, because all option elements will have the same value and text, so try

while($row = $db->fetch($query)) {
    printf('<option value="%s"%s>%s</option>',
            $row['country-code'],
            ($row['country-code'] === $selection) ? ' selected="selected" ' : '',
            row['country-name']);
}

With $selection being anything you want to compare against. Replace the keys in $row with appropriate keys from in your database.

Note: The usual disclaimers about securing your output apply

Gordon