Jump to content

Question about str_replace()


MinusMyThoughts
 Share

Recommended Posts

so, i've put all my update pages in a folder called "update" for organizational purposes......now, that's all working fine, except i'm trying to display my database content on my update pages, including the URL of pictures that my user uploads to accompany things like testimonials and article posts......i use a function with the uploading of the image to save a database entry linking to it that looks like this:

<img src="images/praisePics/img.jpg">

...the problem is that when i call that from the "update" folder, i need to use

<img src="../images/praisePics/img.jpg">

...so i had the idea to use explode("/",$pictureURL) to break it into an array that i called $pictureURL that (at least i think) should look like this:$pictureURL[0] = <img src="images$pictureURL[1] = praisePics$pictureURL[2] = img.jpg">...so i'm running str_replace('src="images','src="../images',$pictureURL) to make the changes to the URL......then i'm running the whole thing through this for() loop:

	$picture = '';	for($i = 0; $i <= count($pictureURL) - 1; $i++)	  {	  $picture = $picture . $pictureURL[$i] . '/';	  }	$picture = $picture . $pictureURL[$i];

...so the whole thing looks like this:

	$pictureURL = explode("/",$display['praisePic']);	str_replace('src="images','src="../images',$pictureURL);	$picture = '';	for($i = 0; $i <= count($pictureURL) - 1; $i++)	  {	  $picture = $picture . $pictureURL[$i] . '/';	  }	$picture = $picture . $pictureURL[$i];

...i know i'm doing something wrong, but i'm at a loss. anyone feeling helpful? thanks!love,jason

Link to comment
Share on other sites

$pictureURL = explode("/",$display['praisePic']);	str_replace('src="images','src="../images',$pictureURL);	$picture = '';	for($i = 0; $i <= count($pictureURL) - 1; $i++)	  {	   	  $picture .= $pictureURL[$i];		   if ( $i < 2 )  {  $picture .= '/';}	  }	echo $picture;

Might work. The ".=" is the concatenation operator.

Link to comment
Share on other sites

after a little alteration, it worked great!...here's what i'm using that is currently working:

	$pictureURL = $display['praisePic'];	$pictureURL = explode("/",$display['praisePic']);	str_replace('src="images','src="../images',$pictureURL);	$picture = '<img src="../images/';	for($i = 1; $i <= count($pictureURL) - 1; $i++)	  {	  	  $picture .= $pictureURL[$i];		if ( $i < 2 )  {  $picture .= '/';}	  }

...thanks for your help!love,jason

Link to comment
Share on other sites

There's an analagous function to explode that turns an array back into a string, with a "glue" character between each array piece. You can use either implode or join. join is an alias for implode, so it's probably better to use implode.http://www.php.net/manual/en/function.implode.php

	$pictureURL = $display['praisePic'];	$pictureURL = explode("/",$display['praisePic']);	str_replace('src="images','src="../images',$pictureURL);	$picture = implode("/", $pictureURL);

Link to comment
Share on other sites

i didn't have any luck with that......for some reason, it doesn't seem to be replacing anything. i think the way my host is set up is funky, though, because str_ireplace() gives me an error as a "call to undefined function."...thanks, though!love,jason

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...