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
    New Coder
    Join Date
    Jul 2006
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    repeating problem

    hello, I have a small question regarding PHP.

    I have this snippet of code that I need to modify:

    PHP Code:
            $preg "/((?<!\")(http|https|ftp)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?\/?([a-zA-Z0-9\-\._\?\,\\'\/\\\\\+&amp;%\$#\=~])*)/i";
            if (
    preg_match_all($preg$comment$matches)) {
                foreach (
    $matches[0] as $match) {
                    
    $match_str str_replace(".""\."$match);
                    if (
    preg_match('/jpg|png|jpeg/i'substr($match, -5))) {
                        
    $comment preg_replace("!".$match_str."!"'<img src="\0">'$comment);
                    } else {
                        
    $comment preg_replace("!".$match_str."!"'<a rel="nofollow" target="_blank" href="\0">\0</a>'$comment);
                    }
                } 
    what it does is that it is being used for a comment textarea box. so that whenever a user inputs a URL or image URL like http://www.google.com or http://www.google.com/image.jpg, it will automatically be parsed into an url or image.

    one single URL or image parses correctly. so if I put the following into the textarea:

    Code:
    http://www.google.com
    it parses into:

    Code:
    <a rel="nofollow" target="_blank" href="http://www.google.com">http://www.google.com</a>
    or when I place a single image URL into the textarea:

    Code:
    http://www.google.com/image.jpg
    it parses into:

    Code:
    <img src="http://www.google.com/image.jpg">
    my problem, however, is that if there are multiple hyperlinks or image URL's in the textarea, like this:

    Code:
    http://www.google.com
    http://www.google.com
    it parses into:

    Code:
    <a rel="nofollow" target="_blank" href="<a rel="nofollow" target="_blank" href="http://www.google.com">http://www.google.com</a>"><a rel="nofollow" target="_blank" href="http://www.google.com">http://www.google.com</a></a><br />
    <a rel="nofollow" target="_blank" href="<a rel="nofollow" target="_blank" href="http://www.google.com">http://www.google.com</a>"><a rel="nofollow" target="_blank" href="http://www.google.com">http://www.google.com</a></a>
    instead, it should be:

    Code:
    <a rel="nofollow" target="_blank" href="http://www.google.com">http://www.google.com</a><br />
    <a rel="nofollow" target="_blank" href="http://www.google.com">http://www.google.com</a>
    and if there are multiple image URLs, like this:

    Code:
    http://www.google.com/image.jpg
    http://www.google.com/image.jpg
    it would be parsed into something like:

    Code:
    <img src="<img src="http://www.google.com/image.jpg">"><br />
    <img src="<img src="http://www.google.com/image.jpg">">
    the best way to explain this would be like "a URL within a URL" or "an image within an image".

    any help is appreciated. I've tried to explain this as best as I could, but if there are any misunderstandings, please let me know.

    Thanks!
    Last edited by abcdefGARY; 02-26-2008 at 07:07 AM.

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    This example might help a little:
    PHP Code:
    $subject = <<<END

    http://www.google.com/

    http://www.google.com/logo.jpg


    END;

    $replace = array(
                    
    "#(?<!\")(https?://[^ ]+\.jpg|png|jpeg|gif)#ie" => "'<img src=\"' . trim('\\1') . '\" />'",
                    
    "#(?<!\")(http://[^ ]+)#ie" => "'<a href=\"' . trim('\\1') . '\">' . trim('\\1') . '</a>'",
                    );

    $subject preg_replace(array_keys($replace), array_values($replace), $subject); 
    Outputs:
    Code:
    <a href="http://www.google.com/">http://www.google.com/</a>
    
    <img src="http://www.google.com/logo.jpg" />

  • Users who have thanked Inigoesdr for this post:

    abcdefGARY (02-27-2008)

  • #3
    New Coder
    Join Date
    Jul 2006
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    This example might help a little:
    PHP Code:
    $subject = <<<END

    http://www.google.com/

    http://www.google.com/logo.jpg


    END;

    $replace = array(
                    
    "#(?<!\")(https?://[^ ]+\.jpg|png|jpeg|gif)#ie" => "'<img src=\"' . trim('\\1') . '\" />'",
                    
    "#(?<!\")(http://[^ ]+)#ie" => "'<a href=\"' . trim('\\1') . '\">' . trim('\\1') . '</a>'",
                    );

    $subject preg_replace(array_keys($replace), array_values($replace), $subject); 
    Outputs:
    Code:
    <a href="http://www.google.com/">http://www.google.com/</a>
    
    <img src="http://www.google.com/logo.jpg" />
    ahh, I am unfamiliar with PHP... I have no idea how to fix it based on the example you showed me, but does this work with multiple IMG and URL's for $subject?

    I just need to stop the repeating in the code below... but I have no idea how.

    PHP Code:
            $preg "/((?<!\")(http|https|ftp)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?\/?([a-zA-Z0-9\-\._\?\,\\'\/\\\\\+&amp;%\$#\=~])*)/i";
            if (
    preg_match_all($preg$comment$matches)) {
                foreach (
    $matches[0] as $match) {
                    
    $match_str str_replace(".""\."$match);
                    if (
    preg_match('/jpg|png|jpeg/i'substr($match, -5))) {
                        
    $comment preg_replace("!".$match_str."!"'<img src="\0">'$comment);
                    } else {
                        
    $comment preg_replace("!".$match_str."!"'<a rel="nofollow" target="_blank" href="\0">\0</a>'$comment);
                    }
                } 
    Last edited by abcdefGARY; 02-26-2008 at 07:07 AM.

  • #4
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    nobody here will do all the work for you, if you need it done asap hire someone. other wise take some time to learn.

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    sorry for delay, try this:

    PHP Code:
    <?php

    $comment 
    "http://www.google.com/ comment after
    http://www.google.com/  

    https://gmail.com/mail
    https://yahoo.com/image

    http://www.google.com/image.jpg
    http://www.google.com/image.jpg"
    ;

    $test preg_replace("/\b(https|http|ftp)(\S+)/",
                 
    "<b style=\"color: #00f;\">[url]$1$2[/url]</b>",
                 
    $comment);
    $test preg_replace("/\[url\](.+)\.(jpg|jpeg|png|gif)\[\/url\]/","[img]$1.$2[/img]",
                 
    $test);
    $regf = array("/\[url\]([^\[]+)\[\/url\]/",
              
    "/\[img\]([^\[]+)\[\/img\]/");
    $regr = array("<a rel=\"nofollow\" target=\"_blank\" href=\"$1\">$1</a>",
              
    "<img src=\"$1\">");
    $test preg_replace($regf,$regr,$test);

    $test preg_replace("(\r?\n)","<br/>",$test);
    print 
    $test;
    ?>
    is tested with what is in $comment variable, but you must do more test to be sure.

    best regards

  • Users who have thanked oesxyl for this post:

    abcdefGARY (02-27-2008)

  • #6
    New Coder
    Join Date
    Jul 2006
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts
    hah! I managed to figure out how to stop the repeating problem by playing around and deleting one character after the other and see which one works.

    the problem was in the bolded line:

    PHP Code:
            $preg "/((?<!\")(http|https|ftp)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?\/?([a-zA-Z0-9\-\._\?\,\\'\/\\\\\+&amp;%\$#\=~])*)/i";
            if (
    preg_match_all($preg$comment$matches)) {
                foreach (
    $matches[0] as $match) {
                    [
    B]$match_str str_replace(".""\."$match);[/B]
                    if (
    preg_match('/jpg|png|jpeg/i'substr($match, -5))) {
                        
    $comment preg_replace("!".$match_str."!"'<img src="\0">'$comment);
                    } else {
                        
    $comment preg_replace("!".$match_str."!"'<a rel="nofollow" target="_blank" href="\0">\0</a>'$comment);
                    }
                } 
    this:

    Code:
    $match_str = str_replace(".", "\.", $match);
    should be:

    Code:
    $match_str = str_replace(".", ".", $match);
    but thanks for your suggestions.


  •  

    Posting Permissions

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