0

Any ideas what might be wrong with this regex - it doesn't seem to find anything:

function ad_content($content) {
    if (is_single()) {
    $find = '#<div id=\"attachment_(\d+)\" class=\"wp-caption aligncenter\" style=\"width: (\d+)px\">(.*?)</div>#s';
    $replace1 = '11111';
    $content = preg_replace($find,$replace,$content,1);
    }
    return $content;
}
add_filter ('the_content','ad_content');

I've tried with something basic like

$find = '#attachment#';

and that does work.

When I use the above regex it doesn't replace anything, and gives no errors either. Thus, I suppose it just doesn't find anything. Here's what it should find:

<div id="attachment_167" class="wp-caption aligncenter" style="width: 600px"><a href="http://www.url.com"><img class="size-full wp-image-167" alt="text" src="http://www.url.com" width="600" height="776" /></a><p class="wp-caption-text">text &#8211; text</p></div>

I've tried it at this regex validator and it does match.

ANSWER:

I think I've finally figured it - the_content hook, doesn't seem to apply to my div. Simple as that.

kat
  • 217
  • 3
  • 12
  • 4
    What not use a DOM parser to do this? That would be preferred method. – Mike Brant Sep 20 '13 at 16:33
  • 7
    http://stackoverflow.com/a/1732454/1253312 – 000 Sep 20 '13 at 16:34
  • Because I have no idea what that is. I am THAT new to this. Would you be so kind as to shed some light? – kat Sep 20 '13 at 16:34
  • @kat See [this question](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) for a starting place. Basically you should use some pre-written code (a library) that can do all the heavy lifting for you in searching through your HTML string. – ajp15243 Sep 20 '13 at 16:37
  • OK. I understand, but I don't need to do some complex HTML parsing, in fact something as simple as $find = '#
    #s'; would still do the job, but for some reason it seems too complicated for the regex to match.
    – kat Sep 20 '13 at 16:47
  • Your $find string is '-delimited. Better check if the resulting string has " or \" in it. If the latter (\"), it may not work in the preg_replace() call, in which case drop the \ escapes. – Phil Perry Sep 20 '13 at 16:58
  • @PhilPerry The delimiters are `#`, not single quotes... – Jerry Sep 20 '13 at 17:30
  • Make sure you look at the real "View source" and not Firebug och a dev tool. They usually change the order of attributes and change quotes etc. – Nicholas Ruunu Sep 20 '13 at 17:38

2 Answers2

1

Your regex looks correct to me, really.

When I change $replace1 to $replace, to agree with usage later in the function, and remove the if statement, it seems to work. That is:

function ad_content($content) {
    $find = '#<div id=\"attachment_(\d+)\" class=\"wp-caption aligncenter\" style=\"width: (\d+)px\">(.*?)</div>#s';
    $replace = '11111';
    $content = preg_replace($find,$replace,$content,1);
    return $content;
}

Seems to work as intended. I'm guessing that the $replace1 vs. $replace problem probably isn't in the code your executing (since you are detecting no errors), so are you sure that is_single() is returning true in the context in which you are testing this?

femtoRgon
  • 32,893
  • 7
  • 60
  • 87
  • Yup. $find = '#attachment#'; does work. PS. You are correct on the $replace1 - just a typo. I even simplified it all the way to: $find = '#
    #s';
    – kat Sep 20 '13 at 16:51
0

You should use a domparser to get the content of the "correct" div.

imagine there would be a "div" inside or the div iteself may be nested:

  <div> 
    Something else
      <div id="thisIwantToMatch"> Foo <div>Bar</div> Baz </div>
    Again something else
  </div>

Since the End-Tag does not contain attributes, it is hard - bareley impossible - to find the right one using regex. Using a "lazy" regex will match <div id="thisIwantToMatch"> Foo <div>Bar</div> while a greedy regex will match <div id="thisIwantToMatch"> Foo <div>Bar</div> Baz </div>Again something else</div>

Obviously both cases are not what you want.

dognose
  • 20,360
  • 9
  • 61
  • 107
  • I'd love it to match that... or anything! My HTML is very simple so it won't have any nested divs etc. The real issue is why it doesn't match anything at all? I even simplified it all the way to: $find = '#
    #s'; Still no matches.
    – kat Sep 20 '13 at 16:57