Solved

Php snippet pulling in full url.  I just want relative url

Posted on 2013-12-27
7
256 Views
Last Modified: 2014-08-03
Hello PHP experts,

I am trying to troubleshoot an image link issue.   I have sent an email to the developer and he will eventually get back to me.  But maybe not quickly.

In the meantime I think I have found the root of the issue.

There is some js in the application that is referencing the various images.  But instead of just putting in the relative url it is pulling in the full absolute url.  I want the relative url, not the absolute url.

I know next to nothing about php, but I found this snippet in some javascript in his application.

image_markup: '<img id="fullResImage" src="{path}" />',

Open in new window

I am thinking that "{path}" is built-in php placeholder code for full url.  Perhaps there is some other placeholder other than  "{path}" that will give me just the relative path.

However, as I write this question, I just found another snippet.  This is a php snippet.  And I am thinking that maybe it is controlling the display of the url.  Here it is:
if(strstr($element_setting['image'],"http://")){
	$image_url=$element_setting['image'];
}
else{
	$image_url="../".$element_setting['image'];
}
?>
<h5><?php echo $element_attribute['name'];?></h5>
<div style="max-width:300px;"><img src="<?php echo $image_url;?>"/></div>

Open in new window

As I look at the above snippet I am thinking that it is more likely the absolute url culprit.

Nevertheless, what the current code is giving me (and I don't want) is <img alt="" src="http://www.domainname.com/images/EmployeePhotos/martha.jpg" /></p>

What I want is a relative path, such as <img alt="" src="/images/EmployeePhotos/martha.jpg" /></p>

I thought while I wait for the developer to reply I would ask your thoughts here.  Of course I have a backup of the files.  I thought maybe you can reply quicker than the developer.  After all, this is a holiday weekend, isn't it?

I realize this question may be an exercise in futility without the full code from the application, but I am hoping that the snippets of code may offer a clue to a solution from the EE php experts. :)

Thanks!

Rowby
0
Comment
Question by:Rowby Goren
  • 3
  • 3
7 Comments
 

Assisted Solution

by:PaNes
PaNes earned 250 total points
ID: 39743162
Let's start with the potentially easiest solution:  try changing {path} to {$path}

Now, assuming that didn't work, the beginning of your code is likely causing the problem.
 
if(strstr($element_setting['image'],"http://")){
	$image_url=$element_setting['image'];
}

Open in new window


Your code assigns the file location prefix to your image location.

Essentially, this says that if the image URL includes http://, then just the link as-is.
If it doesn't contain http://, then it goes to your ELSE condition.

else{
	$image_url="../".$element_setting['image'];
}
   

Open in new window


This says that if the file *wasn't* found to have an http:// prefix already, that it should instead be assigned a "../" prefix, where ../ indicates a relative location.  

Good news!  The way strstr works is that it returns the value that *follows* the matching condition, so what you want is doable, I think.

If every image is hosted at www.bobsfinechina.com, then simply change the code from

if(strstr($element_setting['image'],"http://")){
	$image_url=$element_setting['image'];
}

Open in new window


To

if(strstr($element_setting['image'],"http://www.bobsfinechina.com")){
	$image_url="../".strstr($element_setting['image'],"http://www.bobsfinechina.com");
}

Open in new window


note the
 
.

Open in new window

Between "../" and $element_setting['image'];

That basically adds the two together.

So in summary, you are adding ../ to the results of the strstr query function, which should give you the relative path you desire.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 39743608
Comments annotating the code snippet above:

// EXAMINE THE VARIABLE IN THE $element_setting ARRAY AT THE POSITION NAMED 'image'
// IF THIS VARIABLE CONTAINS THE SUBSTRING "http://"
if(strstr($element_setting['image'],"http://")){

    // COPY THE VARIABLE INTO THE $image_url VARIABLE
    $image_url=$element_setting['image'];
}
// IF THIS VARIABLE DOES NOT CONTAIN THE SUBSTRING "http://"
else{
    // PREPEND "../" TO THE VARIABLE AND THEN COPY THE VARIABLE INTO THE $image_url VARIABLE
    $image_url="../".$element_setting['image'];
}
// THE $image_url VARIABLE WILL BE INJECTED INTO THE HTML DOCUMENT BELOW
?>
<h5><?php echo $element_attribute['name'];?></h5>
<div style="max-width:300px;"><img src="<?php echo $image_url;?>"/></div>

Open in new window

What I think we're seeing here is a construct that will allow any explicit URL (one containing HTTP://) to go through unalloyed.  Other URLs will be made relative to the directory one level "up" from this directory.

You may have to experiment with it a bit, but something along these lines might get what you want.

// REMOVE THE EXPLICIT DOMAIN NAME FROM THE VARIABLE IN THE $element_setting ARRAY AT THE POSITION NAMED 'image'
$image_url = str_replace('http://domain.com/', '../', $element_setting['image']);

// THE $image_url VARIABLE WILL BE INJECTED INTO THE HTML DOCUMENT BELOW
?>
<h5><?php echo $element_attribute['name'];?></h5>
<div style="max-width:300px;"><img src="<?php echo $image_url;?>"/></div>

Open in new window

0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 39743695
Hi Ray,

Below is how I implemented your code.  Am not sure if I did it correctly:

defined( '_JEXEC' ) or die;
$element_key=$item_params['element_key'];
$element_setting=$item_params['element_setting'];
$element_attribute=$item_params['element_attribute'];
$element_shortcode=$item_params['element_shortcode'];
	   
if(strstr($element_setting['image'],"http://")){
// REMOVE THE EXPLICIT DOMAIN NAME FROM THE VARIABLE IN THE $element_setting ARRAY AT THE POSITION NAMED 'image'
$image_url = str_replace('http://www.blahblah.edu/', '../', $element_setting['image']);
}
// THE $image_url VARIABLE WILL BE INJECTED INTO THE HTML DOCUMENT BELOW
?>
<h5><?php echo $element_attribute['name'];?></h5>
<div style="max-width:300px;"><img src="<?php echo $image_url;?>"/></div>

Open in new window


I think I did something wrong because below is the result:

<hr id="system-readmore" />[yee_row ex_class=""][yee_column width="1/1" ex_class=""][yee_single_image title="" image="http://www.blahblah.edu/images/HASOM_fp_photos/martha-resized.jpg" etc etc....  

Open in new window


Basically it did not remove the http://www.blahblah.com.

If it looks right I will paste it back in.  But please double check my implimentaiton when you have a chance.

Thanks

Rowby
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39743718
You might want to give it a try this way.  You don't need the if() statement, but you might want this: http://www.php.net/manual/en/function.str-ireplace.php

If this doesn't remove the domain name, there are a few other possibilities.  One is that this is not the code that generates the <img> tag.  The other is that the domain name is not spelled correctly, or contains HTTPS instead of HTTP, etc.

defined( '_JEXEC' ) or die;
$element_key=$item_params['element_key'];
$element_setting=$item_params['element_setting'];
$element_attribute=$item_params['element_attribute'];
$element_shortcode=$item_params['element_shortcode'];
	   
// REMOVE THE EXPLICIT DOMAIN NAME FROM THE VARIABLE IN THE $element_setting ARRAY AT THE POSITION NAMED 'image'
// THE $image_url VARIABLE WILL BE INJECTED INTO THE HTML DOCUMENT BELOW
$image_url = str_ireplace('http://www.blahblah.edu/', '../', $element_setting['image']);
?>
<h5><?php echo $element_attribute['name'];?></h5>
<div style="max-width:300px;"><img src="<?php echo $image_url;?>"/></div>

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39743722
One other thing to check... This piece of code:
<h5><?php echo $element_attribute['name'];?></h5>
<div style="max-width:300px;"><img src="<?php echo $image_url;?>"/></div>

Open in new window

Cannot possibly create this:
<hr id="system-readmore" />[yee_row ex_class=""][yee_column width="1/1" ex_class=""][yee_single_image title="" image="http://www.blahblah.edu/images/HASOM_fp_photos/martha-resized.jpg" etc etc....

Open in new window

0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 39743757
Hello PaNes and Ray,

Well I guess the code is pulling in the url some other way, because it is not replacing the url string.    

Let's hold off for now and hopefully the developer will get back to me this weekend and I will report his "solution".

Stay tuned!

And thanks!

Rowby
0
 
LVL 9

Author Closing Comment

by:Rowby Goren
ID: 40237401
Thanks Ray and Panes,

Here are you points.  Sorry for the delay!

Rowby
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question