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 6 of 6
  1. #1
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts

    confused, preg_replace...

    Hello, here is a snip from my phpBB and this is for the BBCode function that replaces [ img]url_here[ /img] with the users image..

    I was trying to change this so it would allow URLS with ?'s in them...

    for instance, as it stands now if l posted:
    [ img]http://www.example.com/img.jpg?size=full[/ img]

    The ? breaks the [ img] and it doesn't show up.
    So basically l'm trying to make it so that the board will allow ?'s to be posted in [ img] URL's...

    What would l need to change in the code bellow to make this happen?

    PHP Code:
    // [img]image_url_here[/img] code..
        
    $text preg_replace("#\[img\]((http|ftp|https|ftps)://)([^ \?&=\#\"\n\r\t<]*?(\.(jpg|jpeg|gif|png)))\[/img\]#sie""'[img:$uid]\\1' . str_replace(' ', '%20', '\\3') . '[/img:$uid]'"$text); 
    I just dont understand the preg_replace mess ^that thing is...

    thanks for the help

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Hi Samantha,
    To respond to the ^ question, where its currently located its searching for NOT in. So, [^ \?&=\#\"\n\r\t<]*? searches for any character that is not a ? &=#"newline or tab, any number of times. The question mark there isn't nessessary, as its searching for zero or more occurances. This however, is not what your looking to adjus, you looking for after the extension part. Now, I'd need to through something together quickly for you, but ATM I'm not feeling well so I won't be able to add it for you, but after the extension check, you would probably add:
    Code:
    (\.(jpg|jpeg|gif|png))(\?([^\?&=\#\"\n\r\t<]*=[^\?&=\#\"\n\r\t<]*)*)?
    I could trim it down for you, but something of the sorts should work. Where its located should also drop it into your first backreference as well.

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Location
    Texas
    Posts
    472
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm confused too. What exactly does preg_replace() do?
    If you're reading this, it may already be too late!

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    preg_replace searches through a string that you pass it looking for matches that you specify with regular expressions. If it finds a match it replaces it with something else you've specified.

    In this case the preg_replace() is looking for a url pointing to an image specified in a section of text between [img] and [/img] tags and matching a specified format so that it can replace it with the proper format.

  • #5
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts
    Sorry l still can't get it to work, here's what l've tried:

    PHP Code:
    // [img]image_url_here[/img] code..
        
    $text preg_replace("#\[img\]((http|ftp|https|ftps)://)([^ \?&=\#\"\n\r\t<]*?(\.(jpg|jpeg|gif|png))(\.(jpg|jpeg|gif|png))(\?([^\?&=\#\"\n\r\t<]*=[^\?&=\#\"\n\r\t<]*)*)?)\[/img\]#sie""'[img:$uid]\\1' . str_replace(' ', '%20', '\\3') . '[/img:$uid]'"$text); 
    any ideas?

    Samantha Gram

  • #6
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Quote Originally Posted by Fou-Lu
    The question mark there isn't nessessary, as its searching for zero or more occurances.

    I disagree. The question mark, in this case, makes the expression "non-greedy"

    Consider the following string:

    Code:
    <img src="/images/myImg.jpg" alt="this is my image" />
    If I use this regular expression:

    Code:
    /img src=\"(.*)"/
    I will match "/images/myImg.jpg" alt="this is my image"

    On the other hand, if I use
    Code:
    /img src=\"(.*?)"/
    I will match "/images/miImg.jpg"


  •  

    Posting Permissions

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