url parsing - changing bbcode to html and other way round

Discussion in 'PHP' started by liam_d, Oct 24, 2005.

  1. #1
    Hi all, i am still learning php really and the project i am working on is to help me learn, i am just wondering if anyone has a good example or tutorial of parsing urls?
     
    liam_d, Oct 24, 2005 IP
  2. king_cobra

    king_cobra Peon

    Messages:
    373
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    what is the relation betwoeen url parsing and bbcode? cud u make ur question clearer?
     
    king_cobra, Oct 24, 2005 IP
  3. liam_d

    liam_d Active Member

    Messages:
    306
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    80
    #3
    url is a bbcode...
     
    liam_d, Oct 25, 2005 IP
  4. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #4
    just use preg_replace to find and replace the code with html
     
    exam, Oct 25, 2005 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,840
    Likes Received:
    4,542
    Best Answers:
    123
    Trophy Points:
    665
    #5
    bbParser will do what you need, I reckon.
     
    sarahk, Oct 25, 2005 IP
  6. liam_d

    liam_d Active Member

    Messages:
    306
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    80
    #6
    Well i now have this system;

    
    	//urls	
    	$chr_limit = '45';
    	$add = '...';
    	
    	$post_body['body'] = preg_replace("!(http:/{2}[\w\.]{2,}[/\w\-\.\?\&\=\#]*)!e", "'<a href=\"\\1\" title=\"\\1\" target=\"_blank\">'.(strlen('\\1')>=$chr_limit ? substr('\\1',0,$chr_limit).'$add':'\\1').'</a>'", $post_body['body']);
    
    PHP:
    Only thing i need now is one to pick up urls without a http:// before them any ideas to possibly modify this one?
     
    liam_d, Oct 27, 2005 IP
  7. lappy512

    lappy512 Peon

    Messages:
    277
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #7
    why do you want to design your system using BBCODE? to limit it more? It give users more hassle, IMO.
     
    lappy512, Oct 27, 2005 IP
  8. Stanislav

    Stanislav Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    regular expression for a valid url:
    preg_match('/^http\:\/\/(www\.)?([a-z0-9][a-z0-9-]+\.)+[a-z]{2,4}\/?$/', $url)
     
    Stanislav, Oct 27, 2005 IP
  9. liam_d

    liam_d Active Member

    Messages:
    306
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    80
    #9
    Well put it this way lappy...if users can use html they can do all kinds of crazy things...there has to be a point that i stop them.

    Thanks stanislav ill give that a go!
     
    liam_d, Oct 27, 2005 IP
  10. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #10
    You could also add the "i" modifier to what stanislav posted to make it case-insensitive
     
    exam, Oct 27, 2005 IP
  11. liam_d

    liam_d Active Member

    Messages:
    306
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    80
    #11
    I don't actually get how to use preg_match, isn't preg match just for finding? I'm trying to replace...
     
    liam_d, Oct 28, 2005 IP
  12. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I suggested preg_replace for that very reason :) But if I were you, I'd take sarahk's advice.
     
    exam, Oct 28, 2005 IP