|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
[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:
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... |
|
#2
|
|||
|
|||
|
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- |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > [PHP] Getting Certain Text From Variables |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|