根据引用 URL 的查询字符串替换 img src 值 [英] Replace an img src value based on a referring URL's query string

查看:53
本文介绍了根据引用 URL 的查询字符串替换 img src 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Magento 商店,我想根据引用 URL 是否包含特定的查询字符串或查询字符串值 (adnetwork=as) 替换 img 的 src 值,如果不包含则直接离开图像原样.

这是否足够简单,我到处寻找答案,甚至在各种论坛上询问都无济于事.

到目前为止,这是我所拥有的,似乎是正确的语法,只是 Firebug 报告它无法加载图像 URL,它只是显示".路径正确并且已经过测试.

getSkinUrl('images/logo.gif');//从 $_SERVER 数组中获取referrer url,如果为空则为false$referrer = empty($_SERVER['HTTP_REFERER']) ?假:$_SERVER['HTTP_REFERER'];//如果引用存在如果($推荐人){//解析查询的url$query_str = parse_url($referrer, PHP_URL_QUERY);//如果有查询字符串如果(!空($query_str)){//解析它并将数组放入 $componentsparse_str($query_str, $components);//如果设置了adnetwork"组件if(!empty($components['adnetwork'])) {//将 $logo var 设置为 adnetwork 图片源$logo = $this->getSkinUrl('images/logo-no-number.gif');}}}?><div class="header"><img class="shop24" src="<?php echo $this->getSkinUrl('images/shop-24.gif') ?>"alt="一天 24 小时在线购物"/><div class="shopping-bag"><?php echo $this->getChildHtml('topcart')?></div><div id="fplogo"><a href="<?php echo $this->getUrl('') ?>"title="销售盆地 - Clickbasin.co.uk"><img src="<?php echo $logo; ?>"alt="销售盆地 - Clickbasin.co.uk"/></a></div><img src="<?php echo $this->getSkinUrl('images/side_logo_promo.gif') ?>"alt="Promotion" class="side-logo-promo"/><?php echo $this->getChildHtml('topMenu') ?><?php//<?php echo $this->getLogoSrc() ?>

希望能帮到大家.谢谢.

解决方案

你不应该只是回应 $logo 吗?您将图像分配给 $logo,但您从未为 $logo 设置任何内容.

I have a Magento store and I would like to replace the src value of an img based on whether the referring URL contains a specific querystring or querystring value (adnetwork=as) and if not simply leave the image as it is.

Is this simple enough to do, I've searched everywhere for the answer, and even asked on various forums to no avail.

Here's what I have so far, seems like the correct syntax, just Firebug reports that it is unable to load the image URL, and it simply shows "". The paths are correct and have been tested.

<?php

// Path for default image source
$logo = $this->getSkinUrl('images/logo.gif');

// Get the referrer url from the $_SERVER array, or false if it's empty
$referrer = empty($_SERVER['HTTP_REFERER']) ? false : $_SERVER['HTTP_REFERER'];

// If the referrer exists
if($referrer) {

    // parse the url for the query
    $query_str = parse_url($referrer, PHP_URL_QUERY);

    // If there's a query string
    if(!empty($query_str)) {
        // Parse it and put the array into $components
        parse_str($query_str, $components);

        // If the "adnetwork" component is set
        if(!empty($components['adnetwork'])) {
            // Set the $logo var to the adnetwork image source
            $logo = $this->getSkinUrl('images/logo-no-number.gif');
        }
    }
}

?>

<div class="header">

    <img class="shop24" src="<?php echo $this->getSkinUrl('images/shop-24.gif') ?>" alt="Shop Online 24 Hours a Day"/>

    <div class="shopping-bag"><?php echo $this->getChildHtml('topcart')?></div>

    <div id="fplogo"><a href="<?php echo $this->getUrl('') ?>" title="Sale Basins - Clickbasin.co.uk"><img src="<?php echo $logo; ?>" alt="Sale Basins - Clickbasin.co.uk" /></a></div>

    <img src="<?php echo $this->getSkinUrl('images/side_logo_promo.gif') ?>" alt="Promotion" class="side-logo-promo"/>

    <?php echo $this->getChildHtml('topMenu') ?>

    <?php //<?php echo $this->getLogoSrc() ?>

</div>

I hope you can help guys. Thanks.

解决方案

Shouldn't you be simply echoing $logo? you are assigning the image to $logo but you never set anything to $logo.

这篇关于根据引用 URL 的查询字符串替换 img src 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆