【问题标题】:Make clickable links using indices of associative array使用关联数组的索引创建可点击的链接
【发布时间】:2019-08-20 09:44:04
【问题描述】:

我有一个这样的关联链接数组

$my_links = [
       'city 1' => 'http://link1',
       'city 2' => 'http://link2',
       'Head Office' => 'http://link3'
    ];

还有一些像这样的html。 html由脚本动态生成(wordpress博客内容)

<p>
   You can visit our stores at City 1 
   and City 2, 
   or visit our Head office.
</p>

必需的输出:使用上述array 的索引制作可点击链接

<p>
   You can visit our stores at <a href="http://link1">City 1</a>
   and <a href="http://link2">City 2</a>, 
   or visit our <a href="http://link3">Head office</a>.
</p>

如何使用PHP 和/或JQuery 实现此目的?

【问题讨论】:

  • 这样做&lt;p&gt; You can visit our stores at &lt;a href="&lt;?php echo $my_links['city 1'];?&gt;"&gt;City 1&lt;/a&gt; and &lt;a href="&lt;?php echo $my_links['city 2'];?&gt;"&gt;City 2&lt;/a&gt;, or visit our &lt;a href="&lt;?php echo $my_links['Head Office'];?&gt;"&gt;Head office&lt;/a&gt;. &lt;/p&gt;
  • 感谢您的快速回复和编辑。 html 由脚本动态生成(来自 wordpress 博客内容)。更新了我的问题。

标签: php jquery arrays wordpress hyperlink


【解决方案1】:

如果您的内容来自 PHP,请使用 PHP 的 str_replace() 函数。

这个函数的三个参数搜索的目的:

  1. 搜索:字符串或数组

  2. 替换:字符串或数组

  3. 实际字符串:字符串

如果searchreplace 是数组,它们的元素计数应该相同。

现在,找到像 City 1、City 2 和 Head Office 这样的字符串段,并通过添加 &lt;a href="... 来替换它们。

代码:

<?php
$my_links = [
       'city 1' => 'http://link1',
       'city 2' => 'http://link2',
       'Head Office' => 'http://link3'
    ];
$content = '<p>
   You can visit our stores at City 1 
   and City 2, 
   or visit our Head office.
</p>';

$find = ['City 1', 'City 2', 'Head office'];
$replace = [
'<a href="'.$my_links['city 1'] . '">City 1</a>',
'<a href="'.$my_links['city 2'] . '">City 2</a>',
'<a href="'.$my_links['Head Office'] . '">Head Office</a>',
 ];
echo  str_replace($find, $replace, $content);
?>

快速更新:

请使用str_ireplace() 而不是上面讨论的str_replace(),因为我们有不区分大小写的比较,例如city 1City 1。此函数的工作方式与str_replace() 相同,只是不区分大小写。

【讨论】:

    【解决方案2】:
    <?php 
    
    $my_links = [
           'city 1' => 'http://link1',
           'city 2' => 'http://link2',
           'Head Office' => 'http://link3'
        ];
    
    
    $str = "<p>
       You can visit our stores at City 1 
       and City 2, 
       or visit our Head office.
    </p>";
    
    
    foreach ($my_links as $link_title => $link) {
        $str = str_ireplace($link_title,"<a href='$link'>".ucwords($link_title)."</a>",$str);
    }
    
    echo $str;
    

    遍历您的$my_links。找到字符串中存在的链接标题,并使用str_ireplace() 将链接标题替换为锚标记。

    【讨论】:

    • 非常感谢!
    【解决方案3】:

    你可以试试这个

    <p>
       You can visit our stores at <a href="<?=$my_links['city 1']?>">City 1</a>
       and <a href="<?=$my_links['city 2']?>">City 2</a>, 
       or visit our <a href="<?=$my_links['Head Office']?>">Head office</a>.
    </p>
    

    【讨论】:

    • 感谢您的快速回复。 html 由脚本动态生成(来自 wordpress 博客内容)。更新了我的问题。
    【解决方案4】:

    只需使用数组引用:

    <p>
       You can visit our stores at <a href="<?php echo $my_links['city 1']; ?>">City 1</a>
       and <a href="<?php echo $my_links['city 2']; ?>">City 2</a>, 
       or visit our <a href="<?php echo $my_links['Head Office']; ?>">Head office</a>.
    </p>
    

    【讨论】:

    • 感谢您的快速回复。 html 由脚本动态生成(来自 wordpress 博客内容)。更新了我的问题。
    猜你喜欢
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 2016-11-03
    相关资源
    最近更新 更多