SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Negating an entire string in a regex

    Here's my problem: I'm trying to match any image tag whose URI isn't absolute (i.e. doesn't begin with http://).

    Code:
    <img src="[^http://]
    That won't work the way I want: it doesn't negate the string 'http://;' it negates either an h, t, p, :, or /. I thought this might work:

    Code:
    <img src="[^(http://)]
    ...but paretheses are taken literally inside brackets, so that won't do what I want either.

    Any ideas? Thanks for your time...

  2. #2
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    easy way out would be negate the return result of your matching function call...

    here's an example for preg_match():
    PHP Code:
    if( !preg_match("|<img src=\"http://|"string) ) 

  3. #3
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Moved to a more appropriate forum

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seanf: Wha? I'd say this is a fairly advanced topic...

    Well anyway, thanks for the input, redemption, but that would require going through them all separately, which kind of defeats the purpose of using a regex... I'm now trying solution using the str_* functions.

  5. #5
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by ThreeDee
    Seanf: Wha? I'd say this is a fairly advanced topic...
    This is more of a forum for asking coding questions, the advanced forum is for discussions of things like OOP and sockets etc.

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see. I'll know for next time.

  7. #7
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,322
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    here's something i wrote that might be of use to you

    it's not exactly elegant, but *it works*

    not even sure if this is what you're trying to do but it might be - what this does is based on the url that you put into $baseurl ($baseurl is the only variable you need to input) it converts all the relative img src links to absolute ones - doesn't change links or style sheet links - could be modified to do that though in the regex bit

    it'll handle any of the variations of relative urls - ../name or ./name or /name etc.

    Code:
    <?php
    
    function absoluteurl($relativeurl, $baseurl, $htmlbit) {
     if ($relativeurl[0].$relativeurl[1]=='./') $relativeurl=substr($relativeurl,2);
     if ($relativeurl{strlen($relativeurl)-1}=='/') $relativeurl=$relativeurl.'x';
     $relativeurlarray=explode("/",$relativeurl);
     $baseurlarray=explode("/",$baseurl);
     array_pop($baseurlarray);
     if ($relativeurlarray[0]=='') $absoluteurl=$baseurlarray[0].'//'.$baseurlarray[2].$relativeurl;
     elseif ($relativeurlarray[0]=='..') {
      while ($relativeurlarray[0]=='..') {
       $height++;
       $temp=array_shift($relativeurlarray);
      }
      while ($relativeurlarray) { 
       $newrelativeurl.='/'.array_shift($relativeurlarray); 
      }
      for ($i=1; $i<=$height; $i++) {
       array_pop($baseurlarray);
      }
      $absoluteurl=$baseurlarray[0].'/';
      array_shift($baseurlarray);
      array_shift($baseurlarray);
      while ($baseurlarray) { 
       $absoluteurl.='/'.array_shift($baseurlarray); 
      } 
      $absoluteurl.=$newrelativeurl;
     }
     else {
      $absoluteurl=$baseurlarray[0].'//';
      array_shift($baseurlarray);
      array_shift($baseurlarray);
      while ($baseurlarray) { 
       $absoluteurl.=array_shift($baseurlarray).'/'; 
      } 
      $absoluteurl.=$relativeurl;
     }
     $htmlbit=str_replace("\\","",$htmlbit);
     $absoluteurl=$htmlbit.$absoluteurl.'"';
     return $absoluteurl;
    }
    
    // $baseurl - the url of the page that you want to convert img src relative links to absolute
    $baseurl='http://www.bbc.co.uk/worldservice/index.shtml';
    
    // grabs the html page specified in $baseurl
    $text=join("",file("$baseurl"));  
    // absolutises the relative img src links
    $newtext=preg_replace("/(src=[\s]*[\"'])(?!http\:\/\/)([^\"^']+)([\"'])/ie", "absoluteurl('$2', '$baseurl', '$1')", $text);
    
    echo $newtext; //prints out all the html with img src relative urls converted to absolute urls
    
    ?>

  8. #8
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ThreeDee, try this
    Code:
    preg_match_all('/<img src="(?<!http:\/\/)([^"]+)/i',$your_string,$matches);
    print_r($matches[1]);
    $matches[1] should contain all the relative URIs.

    Paul


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
  •