SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Mar 2008
    Location
    Jerusalem
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to extract a link from a string?

    Hi
    How are you?
    I have a form that have a text field for a url of an image from Picasa
    The url of the image is hidden in the string generated by Picasa "Embed image"

    Code:
    <table style="width:auto;"><tr><td><a href="http://picasaweb.google.com/lh/photo/fB_OHdaSC9uFgZM_pFOwVg"><img src="http://lh6.ggpht.com/er.reshef/SHTBw1V2VsI/AAAAAAAACck/W-KnXb7Re5Q/s144/1%20042.jpg" /></a></td></tr><tr><td style="font-family:arial,sans-serif; font-size:11px; text-align:right">From <a href="http://picasaweb.google.com/er.reshef/394633">בתים לשימור 394633</a></td></tr></table>
    How do I extract the url in the src="http://*****.jpg" and save only this to the database?
    I guess I need a text field for the Picasa code
    and a form hidden field for for the url extracted from the picasa code
    ???

    With Ruby this can be done like this:
    a='Picasa code'
    a[%r{src="(.*)" /></a>}, 1]

    but I prefer to do this with Javascript

    I use this for a Real Estate web site that you can see here
    http://www.5354.co.il (Its in Hebrew)
    all pics are from Picasa
    Right now I copy the code from picasa to a text file and copy the image url from there manualy

    Thank you for your help

    Erez

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    Code:
    text.replace(/^.*?src="(.*?)".*?$/,'$1');

  3. #3
    SitePoint Member
    Join Date
    Mar 2008
    Location
    Jerusalem
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks oddz,
    This is what I did,
    It is working great,

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    <title>Untitled Page</title>
    </head>

    <script type="text/javascript"><!--//--><![CDATA[//><!--
    function extracturl()
    {
    var box = document.forms[0].picasa ;
    var stringurl = box.value ;
    if (stringurl != "")
    {
    var result = stringurl.replace(/^.*?src="(.*?)".*?$/,'$1');
    document.forms[0].pic_url.value = result ;
    }
    }
    //--><!]]></script>
    <body>
    <form name="example" action="#" onsubmit="extracturl(); return false">
    <input size="100" name="picasa" /><br />
    <input type="submit" value="Submit form" /><br />
    <input size="100" name="pic_url"><br />
    </form>
    </body>
    </html>
    This is a very good resource to: http://www.quirksmode.org/js/formex.html


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
  •