Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Adding http://domain.com to information in a database
Old 10-22-2009, 11:37 AM Adding http://domain.com to information in a database
Junior Talker

Posts: 2
Trades: 0
Good Morning,
I am working on creating a function that adds http://domain.com to an image/link source. Example:
I have this bit of code in a MySQL database:
here is some random text with an image<img src="http://www.webmaster-talk.com/images/image.png" alt="" /> and a <a href="index.php">link</a> and some more random text.

I would like to turn it into something like this:
here is some random text with an image<img src="http://domain.com/images/image.png" alt="" /> and a <a href="http://domain.com/index.php">link</a> and some more random text.

I am currently using str_replace like this: str_replace("<img src=\"", "<img src=\"http://domain.com", $text);

I have thought about that and I will run into a problem if I am adding http://domain.com to every place even if one already exists (so I would end up with something like this: <img src="http://domain.com/http://domain.com/images/image.png" alt="" />

How do I do this correctly?

Thanks,
Mike
mightymouse3062 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-24-2009, 03:38 AM Re: Adding http://domain.com to information in a database
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Step 1. Remove http://domain.com/ from links and img src that have it so that all your urls become relative.
Step 2. Add http://domain.com/ just everywhere because now there is no way to get dupes.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 10-25-2009, 12:30 AM Re: Adding http://domain.com to information in a database
Junior Talker

Posts: 2
Trades: 0
That makes sense however what if there is a link to an external site? How would I handle that?

Thanks,
Mike
mightymouse3062 is offline
Reply With Quote
View Public Profile
 
Old 10-25-2009, 01:31 AM Re: Adding http://domain.com to information in a database
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
If you replace only http://domain.com/ then links to other domains will remain untouched.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 10-25-2009, 08:38 AM Re: Adding http://domain.com to information in a database
Average Talker

Posts: 24
Location: Germany
Trades: 0
Quote:
Originally Posted by mightymouse3062 View Post
That makes sense however what if there is a link to an external site? How would I handle that?

Thanks,
Mike
That's a little bit more complicated.

I think you could do it like this:
1. Find the position of an occurence of the string <img src="http://
2. Go to that position and remove anything between " and the third / (including the third / )
3. str_replace("<img src=\"", "<img src=\"http://domain.com", $text);

You definitely need a combination of some different string functions.
__________________

Please login or register to view this content. Registration is FREE
- Register Now and Share Your Thoughts to Create Innovation and Wisdom


Please login or register to view this content. Registration is FREE
- Psychology, Philosophy and Self Development


Please login or register to view this content. Registration is FREE
- Read About All Those Things That Bug Me
Trey Walter is offline
Reply With Quote
View Public Profile Visit Trey Walter's homepage!
 
Reply     « Reply to Adding http://domain.com to information in a database
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.33241 seconds with 12 queries