开发者

PHP - read variables from a array string field

I have a array that looks like this:

  $sites = array('Twitter' => 'http://twitter.com/home?status=$status',
                 'Digg' => 'http://digg.com/submit?phase=2&title=$title',
                 ....
                );

  $status = 'bla bla';
  $title = 'asdasf';

  foreach($sites as $site_name=>$site_url)
    echo '<li><a href="'.$site_url.'">'.$site_name.'</a></li>';

Notice the $status and $title keywords in the array fields. Is there any way I can "map" these keywords to variables开发者_如何学JAVA I set below?

so the output would be:

<li><a href="http://twitter.com/home?status=bla bla">Twitter</a></li>';


Single quoted strings will not perform variable substitution. Set the variables before the array and use double quotes. I also like to use braces for clarity:

$status = 'bla bla';
$title = 'asdasf';

$sites = array('Twitter' => "http://twitter.com/home?status={$status}",
                 'Digg' => "http://digg.com/submit?phase=2&amp;title={$title}",
                 ....
                );


Just assign $status and $title first, then let string interpolation do the work for you when you create the array. It will require a change to double quotes to work. See:

http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing


Why not do this, set the $status and $title first, then append to the array you produce. They are then ready and set ready for when you output the link

$status = 'bla bla';
$title = 'asdasf';

$sites = array('Twitter' => 'http://twitter.com/home?status=' . $status,
    'Digg' => 'http://digg.com/submit?phase=2&amp;title=' . $title,
    ....
    );


foreach($sites as $site_name=>$site_url)
    echo '<li><a href="'.$site_url.'">'.$site_name.'</a></li>';


Wouldn't this work...

$status = 'bla bla';
$title = 'asdasf';

foreach($sites as $site_name=>$site_url){
   echo '<li><a href="'.$site_url.'?status='.$status">'.$site_name.'</a></li>';
}

I'm not sure what you're trying to do with $title


If you can move the code around:

$status = 'bla bla';
$title = 'asdasf';

$sites = array('Twitter' => "http://twitter.com/home?status=$status",
                 'Digg' => "http://digg.com/submit?phase=2&amp;title=$title",
                 ....
                );

Otherwise:

function get_sites($status, $title)
{
  return array('Twitter' => "http://twitter.com/home?status=$status",
                     'Digg' => "http://digg.com/submit?phase=2&amp;title=$title",
                     ....
                    );
}

$sites = get_sites('bla blah', 'asdasf');

As another alternative:

$sites = array('Twitter' => 'http://twitter.com/home?status=$status',
                 'Digg' => 'http://digg.com/submit?phase=2&amp;title=$title',
                 ....
                );

foreach($sites as $site_name=>$site_url)
{
  $site_url = strtr($site_url, array('$status' => 'bla blah', '$title' => 'asdasf'));
  echo '<li><a href="'.$site_url.'">'.$site_name.'</a></li>';
}

I wouldn't recommend the last approach unless there's a lot of arbitrary content to change.

The first is the best if it works for you.


Use nested sprintf if you want to define $status after the $sites declaration:

<?php
// $sites is defined in a bootstrap / settings file ....
$sites = array(
    'Twitter' => 'http://twitter.com/home?status=%s',
    'Digg' => 'http://digg.com/submit?phase=2&title=%s',
);

....

// $status can be dynamic, loaded from a db, etc.
$status = 'omglol';

....

// And output!
foreach ($sites as $name => $url) {
    echo sprintf('<li><a href="%s">%s</a></li>', 
            sprintf($url, $status),
            $name);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜