cut a string php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mekha
Forum Contributor
Posts: 112
Joined: Sat Mar 31, 2012 6:50 am

cut a string php

Post by mekha »

hi... for example i have this link:
http://xxxxxxxxxxxxxxxxxx.com/mikhastar/pages/

how can i cut the link above and echo this:
/pages/....

i mean i want to cut and echo every thing after mikhastar ...because mikhastar is a fixed word....but the xxxxxxxxxx.com is not fixed!....
any help please?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: cut a string php

Post by requinix »

parse_url would be a great place to start.
mekha
Forum Contributor
Posts: 112
Joined: Sat Mar 31, 2012 6:50 am

Re: cut a string php

Post by mekha »

hi man, how can i looping $mysqli insert query with for() loop?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: cut a string php

Post by requinix »

I don't think parse_url() can help with that.

General Posting Guidelines. The bit about writing a question carefully is important.
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Re: cut a string php

Post by Live24x7 »

something like:

Code: Select all

$urlparts = explode("/", $url);
will explode the $url into an array $urlparts where each array element will be formed by the occurrence of '/':

Code: Select all

$urlparts[0]=> http:/
$urlparts[1]=> ""
$urlparts[2]=> xxxxxxxxxxxxxxxxxx.com
$urlparts[3]=>mikhastar
$urlparts[4]=>pages
you can then echo $urlparts[4]; which will echo the last part of your string.
though there could be more efficient ways of doing this which i may not be aware of.
Post Reply