php - 在数组值内添加空格

标签 php arrays

我怎样才能让这段代码显示带有空格的$anchor。我将在 TextAnchor1 和 TextAnchor2 中添加两个 Text Anchor1 和 Text Anchor。谢谢

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

最佳答案

因此,由于您的 $anchor 值可能不是硬编码的,我假设您真正需要的是一个将字符串作为参数并在任何大写字母或数字之前插入空格的函数。

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

稍后,在写入输出时,可以使用 splitWords($anchor) 来代替 $anchor。

关于php - 在数组值内添加空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4520090/

相关文章:

php - 需要一个循环发送功能的想法

安卓字符串 : Find Number and Create New String

c++ - cin.getline 在 for 循环中搞砸了

java - 用空数据拆分字符串

arrays - Jekyll - 在两个整数变量给定范围内的 for 循环语法

php - cURL 需要代理身份验证

php - Facebook 身份验证实现

php - 在 mysql 表名中使用 php uniqid 会导致偶尔出现错误 - 如何避免它?

php - 自动将文件路径存储在mysql数据库中

php - 从 paypal 数组获取状态