Retrieving data from a string

Discussion in 'PHP' started by slaydragon, May 12, 2008.

  1. #1
    I have a string containing this data

    i would like to retrieve the data "xxxxxxx " enclosed with the youtube bbcode.. how do i fo that? :confused:

    please enlighten me.. thanks
     
    slaydragon, May 12, 2008 IP
  2. french-webbie

    french-webbie Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $string = 'haha haha haaha haha haha [youtube]xxxxxxx[/youtube]';
    preg_match('/(\[youtube\])(.*?)(\[\/youtube\])/i', $string, $matches);
    $matches[2] contains what you are looking for
     
    french-webbie, May 12, 2008 IP
  3. WeedGrinch

    WeedGrinch Active Member

    Messages:
    1,236
    Likes Received:
    73
    Best Answers:
    0
    Trophy Points:
    90
    #3
    Or you can use the str_replace function, to replace "xxxxxxx" with "[youtube]xxxxxxx[/youtube]"
     
    WeedGrinch, May 13, 2008 IP
  4. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #4
    hopefully this works... coded right off my head...

    
    // original string contents
    $string = "haha haha haaha haha haha [youtube]xxxxxxx[/youtube] haha haha haha haha";
    
    // $result will contain "xxxxxxx[/youtube] haha haha haha haha"
    $result = substr($string, strpos($string, "[youtube]") + 9);
    
    // $result will contain "xxxxxxx"
    $result = substr($result, 0, strpos($result, "[/youtube]"));
    
    Code (markup):
     
    daboss, May 13, 2008 IP