Need regular expression help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DigitalPimp
    Confirmed User
    • Jun 2003
    • 512

    #1

    Need regular expression help

    I got the following to match an image tag with a path beginning with a / and replace it with a variable called domain that ends with a /

    $a = eregi_replace(" src=\"/"," src=\"$domain/",$a);

    I want to know how I can write one or more new lines to match a similar image tag path that does not begin with a / and does not begin with a http for example

    <img src="images/someimage.gif">
  • GrouchyAdmin
    Now choke yourself!
    • Apr 2006
    • 12085

    #2
    Why don't you just do this in .htaccess, or set a <base href=.../>? What you're trying to fix shouldn't really be done that way. However, you can src=\"(/?) to just assume another localized request.

    If you really want a semi-decent way to grab and rewrite your links:
    Code:
    $preg = "/[\s]+[^>]*?src[\s]?=[\s\"\']+(.*?)[\"\']+.*?>/i";
       preg_match_all($preg,  $all_of_content_here, $outvar, PREG_PATTERN_ORDER);
    Then just walk that to fix all hrefs. You can preg, or ereg if you want. I'll try to make that easy for ya with the following sample
    Code:
    foreach ($outvar as $imgsrc) {
      if (eregi($domain, $imgsrc)) {
       // is complete
      } elseif (eregi("^/", $imgsrc)) {
      // starts with a /
      } else {
      // assume it's local
      }
      }
    Last edited by GrouchyAdmin; 04-26-2008, 01:45 PM.

    Comment

    • DigitalPimp
      Confirmed User
      • Jun 2003
      • 512

      #3
      Thanks for the tips, trying now.

      Comment

      Working...