开发者

Add spaces inside array values

how can i make this code display $anchor with spaces. I would have Text Anchor1 and Text Anchor two insitead of TextAnchor1 and TextAnchor2. Thank you

$currentsite = get_bloginfo('wpurl');
           $sites = array(
           'TextAnchor1' => 'http://www.开发者_JS百科mysite1.com',
           'TextAnchor2' => 'http://www.mysite2.com'
           );
           foreach($sites as $anchor => $site) 
           {
           if ( $site !== $currentsite ){echo '<li><a href="'.$site.'" title="'.$anchor.'" target="_blank">'.$anchor.'</a></li>';}
           } 


So, as you $anchor values probably aren't hard-coded, I assume what you really need is a function that takes a string as an argument and inserts spaces before any capital letters or numbers.

function splitWords($s){
return trim(preg_replace('/([A-Z0-9])/', ' \1', $s));
}

Later, when writing output, instead of $anchor, you can use splitWords($anchor).


$sites = array(
           'Text Anchor 1' => 'http://www.mysite1.com',
           'Text Anchor 2' => 'http://www.mysite2.com'
           );


Ooh, ooh, my turn.

$sites = array(
           'Text Anchor 1' => 'http://www.mysite1.com',
           'Text Anchor 2' => 'http://www.mysite2.com'
           );


Just change to:

$sites = array(
    'Text Anchor' => 'http://www.mysite1.com',
    'My Text Anchor' => 'http://www.mysite2.com'
);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜