PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 11th, 2003, 05:50 AM
metazero metazero is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 2 metazero User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
[PHP] Getting Certain Text From Variables

Hello,

My question has to do with getting the first certain amount of letters from a variable. For example, lets say i have a variable with a long URL such as:

PHP Code:
 $x "http://bla.com/bla/bla/script?y=la&x=blabla" 


but i only want the the pure URL, in other words URL in this example.

One way i can get it is by storing the first 14 characters (URL) in another variable, but i do not know how to do this.

Can anyone help?

Thanks in advance...

Reply With Quote
  #2  
Old September 11th, 2003, 06:39 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
The substr function should do what you want.

string substr ( string string, int start [, int length])

So from your example $y = substr ($x, 0, 14);

That'll obviously only work though if you know the length of the domain so may not be of much use on its own.

It'll work better in conjunction with strpos to find the location of the first forward slash apart from in 'http://'

int strpos ( string haystack, string needle [, int offset])

To do this you can do as follows -

$slash_pos = strpos ($x, '/', 8);
$y = substr ($x, 0, $slash_pos);

The first line finds the location of the slash that falls after the domain then the second line will grab the domain.

This isn't entirely foolproof though since a domain without an ending slash eg http://www.bla.com will return false to $slash_pos and break the substr function. So if this is a possibility you may want to test for that in between the two lines of code above and only run the second if $slash_pos isn't false.

Hope that helps,

-KM-

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > [PHP] Getting Certain Text From Variables


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT