SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need regex fo image included in anchor tag

    Hi all

    I have an image tag embedded inside and anchor tag this way:
    <a href="#" title="some value'><img alt="this image" border=0' height='10 ' src='thisimage.jpg' width='40' /></a>

    This is to be included on a few sites of mine. Now what I need to do is match this with a regex from these sites pagesource to check if the site has included this code in its page or not? Can someone guide me how to write a regex for the above code?

    Thanks

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You're not matching a pattern, you're matching string, simply use strstr().
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    The string methods don't seem to recognize the variable contents. The code I have tried out is:

    <?php
    $url = 'http://example.com/';
    $haystack = file_get_contents($url);

    /*$haystack="This function does the opposite of explode() function. Explode divides a string into array of strings on a separator, this one joins an array of strings with a separator to form a string. join() and implode() functions are identical.<a href="#" title="some value'><img alt="this image" border=0' height='10 ' src='thisimage.jpg' width='40' /></a>It can be used to find a string or character (needle) within another string (haystack). If match is found, the function returns a substring of haystack from ‘needle’ onwards. It returns false if no match is found.";*/

    $needle = "<a href="#" title="some value'><img alt="this image" border=0' height='10 ' src='thisimage.jpg' width='40' /></a>";

    $pos = strpos($haystack,$needle);

    if($pos === false) {
    // string needle NOT found in haystack
    echo "no";
    }
    else {
    // string needle found in haystack
    echo "yes";
    }

    ?>

    If I use current setting, it does not give me the match found ornot properly. But if I uncomment the $haystack variable and use instead it works. What is the problem. Is it not able to find string from remote location or there is a maximum characters limit for strings.

    Else I need a regular expression for this. Please help me out. Its urgent.

    Thanks

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $needle = "<a href="#" title="some value'><img alt="this image" border=0' height='10 ' src='thisimage.jpg' width='40' /></a>";

    Something wrong with that.

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    The normal idea is to use single quotes to delimit php strings, and double quotes for HTML attributes.

    Code php:
    $needle = '<a href="#" title="some value"><img alt="this image" border="0" height="10"' src="thisimage.jpg" width="40" /></a>';

    However, you are wanting to match against what is on another web site, so you will need to apply one of two techniques.

    1. Escape the quotes that interfere with the PHP string quotes

    Code php:
    $needle = "<a href=\"#\" title=\"some value'><img alt=\"this image\" border=0' height='10 ' src='thisimage.jpg' width='40' /></a>";

    2. Use the heredoc syntax for PHP instead, which has no troubles with quotes

    Code php:
    $needle = <<< EOT
    <a href="#" title="some value'><img alt="this image" border=0' height='10 ' src='thisimage.jpg' width='40' /></a>
    EOT;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •