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
Preg_Replace HTTP url
Old 11-25-2007, 12:20 PM Preg_Replace HTTP url
Novice Talker

Posts: 5
Trades: 0
Hello I am new here,
I am trying to get a url that is changing and convert it. It could be www1.url.com/whatever/../ up to www9.url.com/whatever/../

How can I do this with str_replace or preg_replace?

I am using preg_replace but I can't get it to work.

Code:
$site2 = preg_replace("/http:\/\/www[1-9].url.com\/$domain/i", ''$myurl', $site);

Any ideas?

thanks a lot in advance.
gustos is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-27-2007, 01:52 AM Re: Preg_Replace HTTP url
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
From what you have above, you appear to have an extra quote at the start of $myurl.

Also, 2 points:
1) . (dot) has meaning in a regular expression, so backslash it.
2) $domain is a variable whose contents may have meaning, so it needs to be protected.

Try something like this:

PHP Code:
$site2 preg_replace("/http:\/\/www[1-9]\.url\.com\/".preg_quote($domain,'/')."/i"$myurl$site); 
Now, there are other ways of solving this problem which may be calculationally more efficient (such as extracting the part after the url.com/ and before the next /) and comparing against various cases. Now, you haven't really told me what you're trying to do, so this is all inferred from the code you provided.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 11-28-2007, 01:15 AM Re: Preg_Replace HTTP url
Novice Talker

Posts: 5
Trades: 0
Hello there,
thanks for the reply.

I keep getting error:

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'w'

I fixed it using your .preg_quote($domain,'/').

no errors now but I don't get my replacement.... i guess i am missing something somewhere. will post back

Last edited by gustos; 11-28-2007 at 01:21 AM..
gustos is offline
Reply With Quote
View Public Profile
 
Old 11-28-2007, 01:19 AM Re: Preg_Replace HTTP url
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
That's likely because you didn't escape the / in front of www. When posting errors, copy-and-pasting your code is very helpful.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to Preg_Replace HTTP url
 

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.13939 seconds with 12 queries