1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to select and retrieve particular portion of a url in php? Help Needed.

Discussion in 'PHP' started by hope2life, Aug 5, 2012.

  1. #1
    Hello,

    this is a url as below which exists in my mysql database in a particular field

    ecx.images-amazon.com/images/I/41ucO6VO9TL._SL500_AA300_.jpg

    you can see this below portion in the above url

    41ucO6VO9TL.


    I want to select and retrieve only 41ucO6VO9TL. from the url in the mysql database (ecx.images-amazon.com/images/I/41ucO6VO9TL._SL500_AA300_.jpg)

    I don't want to retrieve the whole url


    Any help regarding this would be greatly appreciated.


    Thanks!
     
    Solved! View solution.
    hope2life, Aug 5, 2012 IP
  2. Thibaut

    Thibaut Well-Known Member

    Messages:
    886
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    140
    #2
    Split it using explode() function
     
    Thibaut, Aug 5, 2012 IP
  3. #3
    What Thibaut wrote is correct but besides that doesn't help much.

    For this particular problem you will need to call the explode function twice. The parameters it takes are the delimiter by which you want to split the string, and the string in question. First you would need to split it by "/" like so:
    $pieces = explode("/", "ecx.images-amazon.com/images/I/41ucO6VO9TL._SL500_AA300_.jpg");
    Code (markup):
    Then you would need to get the last element of the array $pieces which given that you always have 3 "/" will be the 4th element, after which you split it again by "_" and your first element in the array is the string in question you want:
    
    $pieces = explode("_", $pieces[3]);
    $result = $pieces[0];
    
    Code (markup):
    Now $result will have the value of 41ucO6VO9TL. in this particular case.
     
    Alejandro131, Aug 5, 2012 IP
  4. hope2life

    hope2life Active Member

    Messages:
    1,641
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    90
    #4
    Thanks for your great help. Understood now.
     
    hope2life, Aug 5, 2012 IP