Forum Moderators: coopster

Message Too Old, No Replies

preg_replace with rawurlencoded?

It seems like it doesn't works :(

         

BlackDex

11:19 am on Mar 18, 2005 (gmt 0)

10+ Year Member



I have a litle problem with replaceing a string in some HTML code.

the code is:


<img width="240" height="180" src="01%20-%20Raptor%20AMD%20Sempron_image001.jpg">

I want to change the 01%20-%20Raptor%20AMD%20Sempron_image001.jpg becouse the location of the file will be changed after the upload.

the location for instains will be /images/uploaded/01%20-%20Raptor%20AMD%20Sempron_image001.jpg.
After the upload i have the image at the right location and i can access it trough the browser.

How can replace that string so that it will be at the right location in the HTML file?

I tryed a few preg_replace options, but i doesn't changes it. I think this is becouse of the urlencoded string.

Does someone has en example?
The filename i get trough the $_FILE variable.

Thx in advance.

jatar_k

8:50 pm on Mar 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I need a little clarification

so you upload a file via a form, hence the access of the $_FILES superglobal array

why not change the name during upload and save it as
01-Raptor_AMD_Sempron_image001.jpg

and how is the link ending up in the html? I dont really understand the proper sequence of events.

BlackDex

9:10 pm on Mar 18, 2005 (gmt 0)

10+ Year Member



Woeps.. sorry for the incompleet message/request.

I Upload 2 files.
1. The HTML file (01 - Raptor AMD Sempron.html).
2. The image that belongs the the HTML file (01 - Raptor AMD Sempron_image001.jpg).

so i get 2 files.
And i need/want to replace the image file to an standard location like '/images'.
When i want to view that HTML file, i won't see the image becouse the location is diffrent.

The image filename within the HTML file is urlencoded.
Wat i need is some way to change the location/filename within that HTML file. I have the original file and i can urlencode it. But when i try a preg_replace with it, it doesn't change anything. Not even when I do it manualy.

Could it be that for some reason the %20 hex values are a problem?
Can you, or someone else give me an small example about how i can change the location or something?

Thx in advance.

jatar_k

10:33 pm on Mar 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



my guess is the spaces (%20) why not just replace them when it is uploaded with underscores

when the files are uploaded you are saving them somewhere, when you save them use the original filename and fire it through str_replace to swap the spaces

also you really shouldn't use spaces in filenames or directory names.

BlackDex

10:56 pm on Mar 18, 2005 (gmt 0)

10+ Year Member



My problem isn't the filename itself.
But the HTML file in where the location of the image file points to.

I don't care about spaces or stuff like that, and i know it is better to not use it. But hey.. its not that i don't want to :(.

The problem is.. that in the HTML file there is an <img> tag.. in where the src="" attribute with value is. Now i need to change that value. Becouse that points to the image file that has been uploaded.

My prob is.. i can change stuff in the HTML file with preg_replace etc..
But for some reason i can't change that src="(.*?)".
So for example.
i need to change:
<img width="240" height="180" src="01%20-%20Raptor%20AMD%20Sempron_image001.jpg">
In to:
<img width="240" height="180" src="/images/01%20-%20Raptor%20AMD%20Sempron_image001.jpg">

But what i try i can't get it working.
That is my problem.

Warboss Alex

11:56 pm on Mar 18, 2005 (gmt 0)

10+ Year Member



Since you've got the image name, can't you just do a str_replace? All you'd be doing is adding a '/images/' onto the front of it.

Grab the html file into a string with file_get_contents() then do the replacement.

<?php

$str = file_get_contents($html_file);
$str = str_replace($img_name, '/images/'.$img_name, $str);

?>

BlackDex

2:24 pm on Mar 19, 2005 (gmt 0)

10+ Year Member



Thx... that worked.