• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

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

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
Rowby Goren
Asked:
Rowby Goren
  • 3
  • 3
2 Solutions
 
PaNesCommented:
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
 
Ray PaseurCommented:
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
 
Rowby GorenAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Ray PaseurCommented:
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
 
Ray PaseurCommented:
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
 
Rowby GorenAuthor Commented:
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
 
Rowby GorenAuthor Commented:
Thanks Ray and Panes,

Here are you points.  Sorry for the delay!

Rowby
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now