Avatar of Rowby Goren
Rowby Goren
Flag for United States of America asked on

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
PHP

Avatar of undefined
Last Comment
Rowby Goren

8/22/2022 - Mon
SOLUTION
PaNes

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Rowby Goren

ASKER
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
Ray Paseur

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

Ray Paseur

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Rowby Goren

ASKER
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
Rowby Goren

ASKER
Thanks Ray and Panes,

Here are you points.  Sorry for the delay!

Rowby