extracting part of string (2)

Discussion in 'PHP' started by Rasputin, Jun 21, 2008.

  1. #1
    I tried to ask this yesterday but made it sound more complicated than it is and i think no-one understood me.

    Given a string of the form
    "[place]hotel ref 12345"

    how can I echo just the 'place' part and lose the '[',']', and 'lovely hotel'

    Cheers
     
    Rasputin, Jun 21, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    please give a better example of what you want.
     
    EricBruggema, Jun 21, 2008 IP
  3. Rasputin

    Rasputin Peon

    Messages:
    1,511
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I get sent an XML file containing information about travel resorts. One of the bits of information is like

    [place]hotel ref 12345

    eg [New York]5th Avenue hotel

    but I need to print out the 'place' part of the field without the rest of the information. ie I only want New York without the [ or ] or '5th Avenue hotel'

    so at the moment I have $place='[New York]5th Avenue hotel'
    but I only want $place='New York'
     
    Rasputin, Jun 21, 2008 IP
  4. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #4
    $place='[New York]5th Avenue hotel';
    
    $a = explode(']',$place);
    $b = explode('[',$a[0]);
    
    echo $b[1];   //<---- New York 
    PHP:
     
    php-lover, Jun 21, 2008 IP
  5. Rasputin

    Rasputin Peon

    Messages:
    1,511
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That looks exactly what I was looking for
    Thanks!
     
    Rasputin, Jun 21, 2008 IP
  6. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #6
    One more solution may help too.

    regards
     
    Vooler, Jun 22, 2008 IP