SitePoint Sponsor

User Tag List

Results 1 to 23 of 23
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    parsing <img> tag

    Hi guys

    i am new to php and need help in pasrsing img tag
    "<img src=\"images/hocnorwich/productInfo.jpg\" alt=\"Sophie Evening Gown\" title=\" Sophie Evening Gown \" width=\"105\" height=\"143\" />"

    i want to change the width and height of image to 50% of current size

    so how can i do this possible by parsing the string or making tokens or might be using some regular expressions (which i dont know how to use)

    and i want to do it in php

    hope to get a swift answer

    cheers
    salman

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps the easiest way would be to load the HTML document into a DOMDocument, modify the desired element(s), and dump it back into an HTML string.

    http://us2.php.net/manual/en/domdocument.loadhtml.php

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi
    brillient suggestion
    i will give it a try in the morning
    its 12 am mid night in uk so got to sleep
    thanks for such a swift answer
    regards
    salman

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $src preg_replace'/(width|height)="\d+"/''$1="50%"'$src ); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes it did work

    i never understand how you guys come up with these sick solutions

    thanks you for saving my day

    cheers
    salman

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by guls_guys View Post
    sick solutions

    A slip of the keyboard?

  7. #7
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi guys

    another small issue
    the image tag that i am getting is with this function
    $src=htmlentities($proImg);//$proImg is the variable that have img tag
    so when i apply this
    $src = preg_replace( '/(width|height)="\d+"/', '$1="50&#37;"', $src );
    to it and
    echo $src;
    it there is no change to it

    but

    when i copy the string in src like this
    $src="<img src=\"images/hocnorwich/productInfo.jpg\" alt=\"Sophie Evening Gown\" title=\" Sophie Evening Gown \" width=\"105\" height=\"143\" />";
    $src = preg_replace( '/(width|height)="\d+"/', '$1="50%"', $src );
    echo $src;


    then it works

    so any thoughts why its not changing in with htmlentities variable. where as the $src with htmlentities function has the same string in it(when view its output in browser)

    thanks
    salman
    Last edited by guls_guys; Jun 20, 2008 at 01:38. Reason: add some extra info

  8. #8
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post

    A slip of the keyboard?
    A sip of the keyboard, perhaps?

  9. #9
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sick = brilliant,excellent my way of admiration

  10. #10
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    the regex you're using in preg_replace looks for one of the following two strings:

    width="nnn"
    height="nnn"

    where nnn is any number (1, 25, 785).

    My guess is htmlentities substitutes the double quotes (") with something else, so preg_replace doesn't match anymore.

    What is the content of $src?

  11. #11
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $proImg is the actual variable that has the contents
    and when i echo $proImg; it outputs an image in the browser
    but when i do this
    $src=htmlentities($proImg);
    it outputs this
    <img src="images/hocnorwich/productInfo.jpg" alt="Sophie Evening Gown" title=" Sophie Evening Gown " width="105" height="143" />

  12. #12
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What happens when you set the second argument for htmlentities to ENT_NOQUOTES ?

  13. #13
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    excellent
    it worked again
    now it is replacing the string by using ENT_NOQUOTES

    Before
    <img src="images/dvd/die_hard_3.gif" alt="Die Hard With A Vengeance Linked" title=" Die Hard With A Vengeance Linked " width="100" height="80" />
    After
    <img src="images/dvd/die_hard_3.gif" alt="Die Hard With A Vengeance Linked" title=" Die Hard With A Vengeance Linked " width="10&#37;" height="10%" />

    as i have got what i wanted and anohter small issue
    but now when i echo
    echo $src;
    it echos the code instead of img so how can i do this?i want to echo an img now
    Last edited by guls_guys; Jun 20, 2008 at 02:12. Reason: more explaination

  14. #14
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)

  15. #15
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    another sick solution

    from the above mentioned link i used this function
    html_entity_decode();

    and it did the trick
    i finally got the solution

    $src=htmlentities($proImg,ENT_NOQUOTES);
    $src = preg_replace( '/(width|height)="\d+"/', '$1="10%"', $src );
    echo $src= html_entity_decode($src);


    you lot are brilliant.

    i am thank full to you guys for helping me out otherwise i would been crashed into old ford chicken truck

    just a small question about how you guys know which regular expression to use and can you point me out any good tutorial explaining the php regular expresions.
    like if want to replace this i will write this and if i want to do this i will write this regular express , i mean how you guys figure it out , you know what i mean.

    thanks again
    salman
    Last edited by guls_guys; Jun 20, 2008 at 07:41.

  16. #16
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by guls_guys View Post
    just a small question about how you guys know which regular expression to use and can you point me out any good tutorial explaining the php regular expresions.
    For regex's I usually come here and ask

    But sometimes I use the following sites:
    http://www.regular-expressions.info/
    http://regexlib.com/
    http://www.fileformat.info/tool/regex.htm

  17. #17
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi guys

    unfortunately there is another problem.

    this is working fine in IE but when view in Firefox the size if half that we view in IE
    what i mean is if i set width and hieght to 50&#37; in IE it displays the 50% of the original image
    but
    in FireFox it displays 50% of the size in IE.
    lets say in IE the size is 1 inch then in firefox it display 0.5 inch aproximatly.

    i dont get this at all.

    the images used in the tag has width 350 and height 475 originally but width and height values in the tag it self are 30% of it 105x143

    --------------this image is originally 350x475 --------------------------------------------------------------note these sizes ----------
    "<img src=\"images/hocnorwich/productInfo.jpg\" alt=\"Sophie Evening Gown\" title=\" Sophie Evening Gown \" width=\"105\" height=\"143\" />"

    so when i change the size to 50%
    i think IE discard the current values it the tag and use images original values and display 50% of the original image size

    where as i think firefox is using the current values in the tag and displaying 50% of it.

    any ideas?

    regards
    salman sadiq
    Last edited by guls_guys; Jun 20, 2008 at 08:33. Reason: got some more problems so had to open the discussion again

  18. #18
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi

    in preg_replace if i use percentage symbol IE display correct size where as Firefox gets it wrong

    where as if i use fixed values without % symbol like 200 both of the browsers shows correct sizes

    so is there a problem that both of the browsers are reading the % symbol wrong and take wrong percentages

    any help appriciated

    regards
    salman sadiq

  19. #19
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Are the windows (IE, Firefox) the same size?
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  20. #20
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah both are 1920x1200

  21. #21
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi guys

    i have found anohter way, i dont know if it is best of the methods but it works, browser independent. so for me its a sick idea

    PHP Code:
        $src=htmlentities($proImg,ENT_NOQUOTES);
        
    $s explode(" "$src);//exploding to get the "src=some/path"
        
    $s =explode ("=",$s[1]);//exploding again to get the path with out word src. only "some/path"
        
    $img$s[1];//only path this time
        
    $pat '/(^"+|"+$)/';//double quote regular expression
        
    $imgWOQ=preg_replace$pat''$img );//removing double quotes to get some/path
        
    $imgWH getimagesize($imgWOQ);
        
    $percent=0.10;//10%
        
    $width=round($imgWH[0]*$percent);
        
    $height=round($imgWH[1]*$percent);
        echo 
    "<img src=\"$imgWOQ\" width=\"$width\" height=\"$height\"/>";//display img now 
    so guys thanks for helping me out and infect motivating me to find the solution even with my limited knowledge.

    thanks
    salman sadiq

  22. #22
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I'm sure there is a better way then this...
    PHP Code:
    <?php

    $src 
    '<img src="images/hocnorwich/productInfo.jpg" alt="Sophie Evening Gown" title="Sophie Evening Gown" width="105" height="143" />';
    preg_match'/src=["\']([^"\']+)/i'$src$tmp_src ); $tmp_src $tmp_src[1];

    //list( $width, $height ) = getimagesize( $tmp_src );
    $width 105$height 143# For testing.
    $width round$width 0.1 ); $height round$height 0.1 );

    $src preg_replace'/(width|height)=["\']([^"\']+)/ie''"\1=\"".($\1).""'$src );

    var_dump$src );
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  23. #23
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah
    it workd too.
    thanks for such indepth answers

    i am really thanksfull to all of you.

    Regards
    salman sadiq


Bookmarks

Posting Permissions

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