通过将字符串应用为 的 href 和文本,将一维字符串数组转换为锚超链接。标签

发布于 2024-12-20 08:33:12 字数 278 浏览 2 评论 0原文

我想用类似的东西包装数组的所有元素,但我不想要很多行或 foreach 循环

$links = array('london','new york','paris');

结果应该是

<a href="#london">london</a>
<a href="#new york">new york</a>
<a href="#paris">paris</a>

I would like to wrap all elements of an array with something like but I don't want a lot of lines or foreach loop

$links = array('london','new york','paris');

The outcome should be

<a href="#london">london</a>
<a href="#new york">new york</a>
<a href="#paris">paris</a>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

贩梦商人 2024-12-27 08:33:12

array_map 怎么样?

$links   = array('london', 'new york', 'paris');
$wrapped = array_map(
   function ($el) {
      return "<a href=\"#{$el}\">{$el}</a>";
   },
   $links
);

演示(点击源码)

没有 PHP > 5.3,你不能使用 lambda 函数,所以你需要这样的东西:

function wrap_those_links($el)  { 
      return "<a href=\"#{$el}\">{$el}</a>"; 
}

$links   = array('london', 'new york', 'paris');
$wrapped = array_map('wrap_those_links', $links);

PHP 5.2 演示< /a> (再次单击“源”)

How about array_map?

$links   = array('london', 'new york', 'paris');
$wrapped = array_map(
   function ($el) {
      return "<a href=\"#{$el}\">{$el}</a>";
   },
   $links
);

Demo (Click source)

Without PHP > 5.3, you can't use a lambda function, so you'll need something like this:

function wrap_those_links($el)  { 
      return "<a href=\"#{$el}\">{$el}</a>"; 
}

$links   = array('london', 'new york', 'paris');
$wrapped = array_map('wrap_those_links', $links);

Demo for PHP 5.2 (Again, click Source)

女中豪杰 2024-12-27 08:33:12

尝试 join('\n', array_map(function($a) { return "$a<\\a>";}, $links) );

Try join('\n', array_map(function($a) { return "<a href=\"#$a\",>$a<\\a>";}, $links));

夜访吸血鬼 2024-12-27 08:33:12

在现代 PHP 版本中,它可以缩短如下。

$wrapper = static fn (string $link): string => \sprintf('<a href="#%1$s">%1$s</a>', $link);
$result = \array_map($wrapper, $links);

On modern PHP versions it could be shorten as follows.

$wrapper = static fn (string $link): string => \sprintf('<a href="#%1$s">%1$s</a>', $link);
$result = \array_map($wrapper, $links);
海夕 2024-12-27 08:33:12

可重复使用的功能。

function array_wrap_template($array, $template, $rep="{{el}}")
{
    return array_map(function($el)use($template, $rep){
        return str_replace($rep, $el, $template);
    }, array_values($array));
}

$links = array('london','new york','paris');
$wrapped = array_wrap_template($links, "<a href=\"#{{el}}\">{{el}}</a>");

Reusable function.

function array_wrap_template($array, $template, $rep="{{el}}")
{
    return array_map(function($el)use($template, $rep){
        return str_replace($rep, $el, $template);
    }, array_values($array));
}

$links = array('london','new york','paris');
$wrapped = array_wrap_template($links, "<a href=\"#{{el}}\">{{el}}</a>");

荆棘i 2024-12-27 08:33:12

另一种尚未提及的技术是使用 preg_replace() ,它将愉快地迭代字符串数组,尽管显然这并不是一个真正需要正则表达式模式的任务。 (演示

$links = array('london','new york','paris');
var_export(
    preg_replace('/.+/', '<a href="#$0">$0</a>', $links)
);

否则,您可以通过调用 urlencode()< 来执行额外的步骤来生成编码的 URL /code> 位于用作 href 值的城市字符串上。

var_export(
    array_map(
        fn($city) => sprintf('<a href="#%s">%s</a>', urlencode($city), $city),
        $links
    )
);

Another technique not yet mentioned is to use preg_replace() -- which will happily iterate an array of strings -- although, obviously this is not a task that actually demands a regex pattern. (Demo)

$links = array('london','new york','paris');
var_export(
    preg_replace('/.+/', '<a href="#$0">$0</a>', $links)
);

Otherwise, you can go that extra step toward generating encoded urls by calling urlencode() on the city string which is used as the href value.

var_export(
    array_map(
        fn($city) => sprintf('<a href="#%s">%s</a>', urlencode($city), $city),
        $links
    )
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文