dsdfd2322 2015-01-24 10:09
浏览 28

'echo'没有连字符的标签和使用str_replace的正确案例?

I have a bit of a tricky situation here....

I am pulling bookmarks from a service called Pinboard using their API's which works great - but - the category of the 'tag' (i.e. bookmark tag) is echoed out in full with hyphens.

The difficulty I am having is that I'd like the $tag in one instance to retain the hyphens (to allow for an anchor link using markup to work) - whilst - changing the same $tag that is echoed in the < h1 >

So for example one of the $tag is 'Latest-News' - and I'd like that $tag to be printed like this:

for the anchor tag: $tag will echo 'latest-news'

for the < h1 > tag: $tag will echo 'Latest News'

Any ideas how this is done?

Something like this might be on the right track (I hope! - I'm still clearly a n00b):

$str = str_replace("-", " ", $tag);
echo $tag;

+++++

        include 'pinboard-api.php';

        $pinboard = new PinboardAPI('myusername', 'xxxxxxx');
        $bookmarks_all = $pinboard->get_all();

        $bookmarks_grouped_by_tags = array();
        foreach ($bookmarks_all as $bookmark) {
            if (! empty($bookmark->tags) && is_array($bookmark->tags)) {
                foreach ($bookmark->tags as $tag) {
                    $bookmarks_grouped_by_tags[$tag][] = $bookmark;
                }
            } else {
                $bookmarks_grouped_by_tags['no_tag'][] = $bookmark; 
            }
        }
        ?>

        <?php foreach ($bookmarks_grouped_by_tags as $tag => $bookmarks) { ?>
            <a name="<?php echo $tag ?>">

         *****   <h1><?php echo $tag ?></a></h1>  *******


            <? foreach ($bookmarks as $bookmark) { ?>
            <div>
                <a href="<?php echo $bookmark->url ?>"><?php echo $bookmark->title ?></a>
            </div>
            <div><?php echo $bookmark->description ?></div>
            <?php } ?>
        <?php } ?> 

++++

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 在matlab中Application Compiler后的软件无法打开
    • ¥15 想问一下STM32创建工程模板时遇到得问题
    • ¥15 Fiddler抓包443
    • ¥20 Qt Quick Android 项目报错及显示问题
    • ¥15 而且都没有 OpenCVConfig.cmake文件我是不是需要安装opencv,如何解决?
    • ¥15 oracleBIEE analytics
    • ¥15 H.264选择性加密例程
    • ¥50 windows的SFTP服务器如何能批量同步用户信息?
    • ¥15 centos7.9升级python3.0的问题
    • ¥15 安装CentOS6时卡住