0

My preg_replace pattern regex code here..

/<img(.*?)src="(.*?)"/

This is my replace code..

<img$1src="'.$path.'$2"

So i want to negate/exlude a condition.. If img tag have a rel="customimg", dont preg_replace so skip it..

Example: Skip This Line

<img rel="customimg" src="http..">

What might add to this regex pattern?

I searched another post, but I couldn't exactly..

hakre
  • 193,403
  • 52
  • 435
  • 836
Bora
  • 10,529
  • 5
  • 43
  • 73
  • 5
    Please don't try to parse HTML using regex. Use [`DOMDocument`](http://php.net/manual/en/class.domdocument.php) or another HTML parser. It may seem to work at first but it will bite you in the ass later. – PeeHaa Oct 23 '12 at 20:29
  • Agree with @PeeHaa. Your code would be more clear and extensible if you used `DOMDocument` to get all `` and tested if they contained `rel="customimg"`. – Jason McCreary Oct 23 '12 at 20:30
  • 1
    For a list of real solutions see: http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-with-php – PeeHaa Oct 23 '12 at 20:31
  • I thought and tried its anyway.. I need just a right regex syntax.. – Bora Oct 23 '12 at 20:44

4 Answers4

1

Add a negative lookahead:

/<img(?![^>]*\srel="customimg")(.*?)src="(.*?)"/
Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145
  • Please post your exact code then, because I don't see why the above shouldn't work, unless you have ``>``s within your ``img`` tag. – Andrew Cheong Oct 23 '12 at 20:39
  • $html = "WEBSITE ALL HTML CODE" $exp = array( '/ – Bora Oct 23 '12 at 21:11
  • I can confirm that it works. If you have spaces before the closing `>` you can add `\s*` to the regex: `]*\srel="customimg")(.*?)src="(.*?)"\s*>` – marlar Oct 23 '12 at 22:07
1

Because src argument may use single or double quotes, I suggest you to use

preg_replace(
  "/(<img\b(?!.*\brel=[\"']customimg[\"']).*?\bsrc=)([\"']).*?\2/i",
  "$1$2" . $path . "$2",
  $string);

Edit:

To add url prefix instead of full url replacement, use

preg_replace(
  "/(<img\b(?!.*\brel=[\"']customimg[\"']).*?\bsrc=)([\"'])(.*?)\2/i",
  "$1$2" . $path . "$3$2",
  $string);
Community
  • 1
  • 1
Ωmega
  • 42,614
  • 34
  • 134
  • 203
  • @m.buettner - Friend, can you help me, please? Why this one is not working >> http://ideone.com/72hCSW but it is okay here >> http://gskinner.com/RegExr/?32ihj ? Please feel free edit my answer and post comment to notify OP. Thank you. – Ωmega Oct 23 '12 at 21:56
  • http://regexr.com?32ii2 >> almost done.. remain a little issue.. you will see replaced path is [src="PATH"].. it must be [src="PATH/images/logo.jpg"].. So not new path.. My $path variable + old image path.. I wont parse img tags that have customimg, if no have, it will be parsed (src="baseurl+imageurl").. – Bora Oct 23 '12 at 22:25
  • @Bora - My http://gskinner.com/RegExr/?32ihj as published in my previous comment is wokring fine, the problem I have is with PHP code. Anyway... You are asking now to not replacing url link, but add some prefix to it, right? Then you have to change end of my pattern from `.*?\2` to `(.*?)\2` and as replacement you need to use `"$1$2" . $path . "$3$2"` >> http://regexr.com?32ii5 – Ωmega Oct 23 '12 at 22:35
1

Because I only see regex "solutions" coming in. Here is the answer using DOMDocument:

<?php
$path = 'the/path';

$doc = new DOMDocument();
@$doc->loadHTML('<img rel="customimg" src="/image.jpgm"><img src="/image.jpg">');
$xpath = new DOMXPath($doc);
$imageNodes = $xpath->query('//img[not(@rel="customimg")]');

foreach ($imageNodes as $node) {
    $node->setAttribute('src', $path . $node->getAttribute('src'));
}

Demo: http://codepad.viper-7.com/uID5wz

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
0

It would seem like it'd be easier/more expressive to do

if(strpos($haystackString, '"customimg"') === false) // The === is important
{
 // your preg_replace here
}

Edit: Thanks for pointing out missing param guys

Martin Lyne
  • 3,157
  • 2
  • 22
  • 28