webmasters
webmasters

Reputation: 5831

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.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>';}
           } 

Upvotes: 2

Views: 1544

Answers (4)

Shakti Singh
Shakti Singh

Reputation: 86406

Just change to:

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

Upvotes: 0

tishma
tishma

Reputation: 1885

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).

Upvotes: 2

Parris Varney
Parris Varney

Reputation: 11478

Ooh, ooh, my turn.

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

Upvotes: 1

Matt
Matt

Reputation: 3848

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

Upvotes: 1

Related Questions