Jump to content


Photo

About String replacement


  • Please log in to reply
6 replies to this topic

#1 demon_athens

demon_athens
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts
  • LocationPiraeus

Posted 08 March 2006 - 08:18 PM

Hi there,

I have a field of a mysql db with this kind of data

EXAMPLE
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
<p>MPLA MPLA MPLA MPLA σαδηασηασδ ηασδ ηασδ ηασδη ασηασδηασδη ασδ ησαδη ασδησαδη ασδ ηασδ ηασδηασδ ηασδγη ασδηασδηασδη ασδ ηασδ ησαδηασηασδ ηασδ ηασδ ηασδη ασηασδηασδη ασδ ησαδη ασδησαδη ασδ ηασδ ηασδηασδ η</p>
<p align="center"><img width="1280" height="1024" src="/UserFiles/Image/AngelinaJolie77ee17_f1280.jpg" alt="" style="width: 498px; height: 395px;" /></p>
<p>ασδγη ηασδ ησαδηασηασδ ηασδ ηασδ ηασδη ασηασδηασδη ασδ ησαδη ασδησαδη ασδ ηασδ ηασδηασδ η</strong></p>[/quote]

This field's data came from a webform with a wysiwyg editor field

I want to replace the " /UserFiles " with an absolute url for example " [a href=\"http://localhost/UserFiles"\" target=\"_blank\"]http://localhost/UserFiles"[/a]

How can I do it? I suppoce there is a string replacement function.The one that I found in php.net its not the thing i want. One way to do it is to do the replacement before inserting the data to the db. Can you help me with it?

The basic problem is that I need all relative links to be absolute in this field..

Thank you very much in advance
May the Force be with you...

#2 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 08 March 2006 - 08:40 PM

I think what you might be looking for is a combination of the replace function you found on php.net .. and regular expressions, which are also available on php.net. I can help you further, but read up first and come back with more specific questions :)

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#3 demon_athens

demon_athens
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts
  • LocationPiraeus

Posted 08 March 2006 - 09:06 PM

Lets make it specific then...

" How can I find a string and replace it with another given string in a variable?"

better?
May the Force be with you...

#4 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 08 March 2006 - 09:16 PM

[!--quoteo(post=352988:date=Mar 8 2006, 09:06 PM:name=demon_athens)--][div class=\'quotetop\']QUOTE(demon_athens @ Mar 8 2006, 09:06 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Lets make it spesific then...

" How can I find a string and replace it with another given string in a variable?"

better?
[/quote]

Ouch. Attitude.

Well let me explain. Regular expressions can help you find the string ESPECIALLY IF IT IS DYNAMIC, I.E. you dont know what will be between src="<here>"

once you find that location OR the data reference "<here>" you can easily replace the data with what you need it to be, and, using the same method, check to see IF it is already an absolute link.

The reason I am not just giving you code is you can easily copy and paste it and not learn anything.. I am not trying to be a teacher, but what happens if you need this help again? That's why I gave you 2 topics to go read on..

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#5 demon_athens

demon_athens
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts
  • LocationPiraeus

Posted 09 March 2006 - 09:19 AM

As I allready said the code I am looking for is always starting with "/UserFiles/". So I know the string tha must be search and replaced.

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]The reason I am not just giving you code is you can easily copy and paste it and not learn anything.. I am not trying to be a teacher, but what happens if you need this help again?
[/quote]

Well, you are trying to be a teacher and I am not so stupid to come back with the same problem.


Anyway my first though was to use Blow to blow the string and then remake it with the new absolute one( after each blow). The only reason I turned to this forum was because it seemed pretty much code ( and maybe heavy) for such an easy function.. So I asked and your answer was " Go and read mister!'

Well, ok. Thanks ( this may explain my "attitude")
May the Force be with you...

#6 lessthanthree

lessthanthree
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts
  • LocationUK

Posted 09 March 2006 - 09:25 AM

does

$output = str_replace("/UserFiles", "http://localhost/UserFiles/", $string);

not do the job?
call me a safe bet, i'm betting i'm not

#7 demon_athens

demon_athens
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts
  • LocationPiraeus

Posted 09 March 2006 - 09:55 AM

YESSSS!!! It's great. thank you very much for this line lessthanthree. Well it's pretty sure now that it was very easy and I didn't understood the examples of php.net.. ( this worries me a bit!)

Thanks again! Have a nice day
May the Force be with you...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users