Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
  1. #1
    ljm
    ljm is offline
    Senior Member ljm's Avatar
    Join Date
    Aug 2006
    Location
    Manchester, England
    Posts
    284
    Member #
    13684
    Liked
    1 times
    I'm not an expert when it comes to these beauties, and I've got a little bit of a problem with one in particular.

    What I intended to do was use [minicode]preg_replace()[/minicode] to go through a database entry, and replace all the images (which are screenshots in the context of the site) with a link to their full size versions. I'm using TinyMCE for the text editor, in which I sacrificed strict XHTML output for admin panel user friendliness. Anyway, for images, it creates this:

    Code:
    <img src='img.jpg' alt='text' width='x' height='y' align='left' />
    I want it to turn into this:

    Code:
    <a href='img-full.jpg' title='alt text'><img src='img.jpg' alt='text' width='x' height='y' align='left' /></a>
    My regular expression is this:

    Code:
    '<img src=\"(.*?[^\.])\.(.*?)\" alt=\"(.*?)\" />'is
    And the intended output is this:

    Code:
    <a href=\"\\1-full.\\2\" rel=\"lightbox\" title=\"\\3\"><img src=\"\\1.\\2\" alt=\"\\3\" /></a>
    It works on some images, but not on others. It seems to ignore those that have alt text, but leaving the attribute empty works fine. The image tag is left untouched by the function, also, when really it should match that code above!

    I think my regular expression is wrong, but I'm not sure how else I could change it (seeing as I want to remove the size attributes from the image, too).

    Thanks in advance for any help!

  2.  

  3. #2
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Try making that alt="([^"]*)". I'm not sure why the question marks are there?

  4. #3
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Question marks = ungreedy.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  5. #4
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Yeah, I know what the question marks do, I'm asking why use them here :-P I'm relatively sure they'll just slow things down unnecessarily, and may be having other unintended consequences.

    You're also not matching the full image tag, since you aren't putting the width and height parameters in your regular expression, and your actual tags may have it in them; is that intentional?

  6. #5
    ljm
    ljm is offline
    Senior Member ljm's Avatar
    Join Date
    Aug 2006
    Location
    Manchester, England
    Posts
    284
    Member #
    13684
    Liked
    1 times
    I noticed that I wasn't matching them fully, so I changed it. Then everything stopped working.

    I'm using [minicode]wordwrap()[/minicode] on the main body of text, with "\n" as the linebreak (to make it look nice in the source). Do you think that might have an adverse effect? The regexp doesn't work if I remove the single line modifier.

    I put the question mark in the expressions because I used a book for reference, and that was in it.

    I'll have a bit more of a fiddle, anyway. I want to do it this way because I'm trying to automate as many mucky bits of the site as I can. I don't want my system's intended users to be faffing about with links to make lightbox work on the images!

  7. #6
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Gotcha. Why not word wrap after doing this replacement?

  8. #7
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Just a regular expression suggestion:
    PHP Code:
    $str "<img src='image_name.jpg' alt='description' width='x' height='y' align='left' />";

    $pattern "/[\w|\s+]+='(.*)\.[a-z]{3}'[\w|\s+]+='(\w*)'.*/";

    preg_match($pattern$str$res);

    echo 
    "<a href='$res[1]-full.jpg' title='$res[2]'>$str</a>"
    S. Rosland

  9. #8
    ljm
    ljm is offline
    Senior Member ljm's Avatar
    Join Date
    Aug 2006
    Location
    Manchester, England
    Posts
    284
    Member #
    13684
    Liked
    1 times
    Quote Originally Posted by Shadowfiend
    Gotcha. Why not word wrap after doing this replacement?
    I have no idea! :ermm:

    I'll give that a go and what not and see if it all works. I've not tried anything yet.

    Oh, and I like your suggestion Rosland, but I don't know how I'd do that when my 'string' is a huge article, not just the image tag.

    Thanks to everyone for the help though!

  10. #9
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    You should be able to use [phpfunction]preg_replace[/phpfunction] with back-references (use \1 and \2 instead of $res[1] and $res[2]) to do it on a full string, I believe.

  11. #10
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Quote Originally Posted by ljm
    Oh, and I like your suggestion Rosland, but I don't know how I'd do that when my 'string' is a huge article, not just the image tag.
    I don't know if this is of any help, but I embedded some image tags in a text to see if that's closer to what you're after.
    The final 'print' command will output the original string, but with all the image tags altered to what you've described above.

    PHP Code:

    $str 
    "<img src='image_name.jpg' alt='description' width='x' height='y' align='left' />
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam fermentum semper nulla.
    <img src='image_name2.jpg' alt='description2' width='x' height='y' align='left' /> 
    Sed tincidunt pharetra metus.<img src='image_name3.jpg' alt='description3' width='x' 
    height='y' align='left' />
    Nam at turpis. Aliquam lacinia erat vel quam.
    <img src='image_name4.jpg' alt='description4' width='x' height='y' align='left' />
    Vivamus iaculis odio sed pede. Etiam urna.
    <img src='image_name5.jpg' alt='description5' width='x' height='y' align='left' />
    Curabitur enim eros, egestas eget, rhoncus ut, blandit eget, ipsum. Fusce id justo. 
    Nullam semper felis sit amet urna.
    Vivamus laoreet. Suspendisse potenti. Donec aliquam rhoncus lectus. Class aptent taciti sociosqu ad 
    litora torquent per conubia nostra, per inceptos hymenaeos. <img src='image_name6.jpg' 
    alt='description7' width='x' height='y' align='left' /> 
    Quisque arcu est, dictum eget, 
    sollicitudin adipiscing, adipiscing eget, arcu."
    ;


    $pattern "/<img(.*)\/>/";


    preg_match_all($pattern$str$res);

    $mod=$res[0];


    $pattern "/[\w|\s+]+='(.*)\.[a-z]{3}'[\w|\s+]+='(\w*)'.*/";
    $replace="a href='$1-full.jpg' title='$2'>".$res[0][$i]."</a>";


    for(
    $i=0$i count($mod); $i++)
    {
        
    $alter[$i]=preg_replace($pattern"a href='$1-full.jpg' title='$2'>".$res[0][$i]."</a>"$mod[$i]);
    }

    print 
    str_replace($res[0], $alter$str); 
    S. Rosland


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 03:03 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com