Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder GSimpson's Avatar
    Join Date
    Aug 2006
    Location
    New Zealand
    Posts
    268
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Parsing Links Like phpBB

    Hey there,

    I've recently taken a run at parsing links using php. I can't quite work out how to do it, so I'll just post what I'm trying to accomplish.

    I have entries within a database, that include text and html. I've got that selected as $content.

    I'm trying to get a function or set of functions to take this, and replace this:

    Code:
    <a href="http://myblog.com/">My Blog!</a>
    with

    Code:
    [ url=http://myblog.com/]My Blog![ /url]
    I just need to work out how to parse the link from within href="" and move it to after [url= then close it with ]. After that take what was within the anchor tags and put it between the [ url=] and [ /url] tags.

    An ideas, or help?

    Thanks everyone, Greatly appreciated.

    Please ignore the spacing on the bb code, it was so that it would display on the forum.
    Last edited by GSimpson; 05-10-2009 at 06:30 AM.
    The internet is my Sandbox, and notepad is my Spade n' Bucket.

  • #2
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    This should do what you asked. The preg functions are the right stuff to be using, and regex is always good to learn.

    Code:
    preg_replace("/<a href=\"(.*?)">(.*?)</a>/", "[ url=$1]$2[ /url]", $content);
    (remove the whitespace in the url tags of course.

  • #3
    Regular Coder GSimpson's Avatar
    Join Date
    Aug 2006
    Location
    New Zealand
    Posts
    268
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks for that.

    I'm just going to take a shot in the dark, and hope it's right, but, is this how I could reverse it, for the database entry?

    (Assuming $content is the BB version, and we want to make it html)

    Code:
    preg_replace("/[ url=(.*?)](.*?)[/ url]/", "<a href=\"$1\">$2</a>", $content);
    Thanks.
    The internet is my Sandbox, and notepad is my Spade n' Bucket.

  • #4
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    Close, on the regex side, you have to escape chars like [ and ] and / so it would be:

    Code:
    preg_replace("/\[ url=(.*?)\](.*?)\[\/ url\]/", "<a href=\"$1\">$2</a>", $content);

  • #5
    Regular Coder GSimpson's Avatar
    Join Date
    Aug 2006
    Location
    New Zealand
    Posts
    268
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Hey thanks for that, I'll be sure to have a read up of it soon.

    I noticed on your first code your posted, it was missing \ before a quote so I added it in and then I got this message:

    Warning: preg_replace() [function.preg-replace]: Unknown modifier 'a' in C:\Docum...

    I was using this code (minus the url whitespaces)

    Code:
    preg_replace("/<a href=\"(.*?)\">(.*?)</a>/", "[ url=$1]$2[ /url]", $content);
    The internet is my Sandbox, and notepad is my Spade n' Bucket.

  • #6
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    oh, you probably have to escape the < and > as well, I just did it off the top of my head.

  • #7
    Regular Coder GSimpson's Avatar
    Join Date
    Aug 2006
    Location
    New Zealand
    Posts
    268
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks man, I did a bit of research, and found some answers on how the syntax works. This is going to help me a lot in future. Thank you so much.
    The internet is my Sandbox, and notepad is my Spade n' Bucket.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •