Jump to content

[SOLVED] preg_match problem


ted_chou12

Recommended Posts

I have a text:

<h3 class="customize">title</h3>
<p class="customize">content</p>
<p class="post-footer align-right customize">
<span class="date">date</span>
</p>

I wish to extract the title, content and date individually, so far, I have got:

preg_match_all("|<h3 class=\"customize\">(.*)?<\/h3>|U", $content, $result, PREG_PATTERN_ORDER);

Which gets the title, but I cant do the same to get the content, does anyone know how?

Thanks,

Ted.

Link to comment
https://forums.phpfreaks.com/topic/132444-solved-preg_match-problem/
Share on other sites

<pre>
<?php
$data = <<<DATA
<h3 class="customize">title</h3>
<p class="customize">content</p>
<p class="post-footer align-right customize">
<span class="date">date</span>
</p>
DATA;

preg_match_all('/\sclass="(?:customize|date)">([^<]+)/', $data, $result, PREG_PATTERN_ORDER);
print_r($result);
?>
</pre>

Hello  :), thanks for the solution, the starting code tag works very well, but the content stops at <br />

An example of the content that I have is:

<h3 class="customize">title</h3>
<p class="customize">content<br /> blah blah .... <a href=""></a><img src />...</p>
<p class="post-footer align-right customize">
<span class="date">date</span>
</p>

But it can only stop at </p>

Thanks again,

Ted

 

<pre>
<?php
   $data = <<<DATA
<h3 class="customize">title</h3>
<p class="customize">content</p>
<p class="post-footer align-right customize">
<span class="date">date</span>
</p>
DATA;

   preg_match_all('%<(\w+)[^>]+class="(?:customize|date)">(.*?)</\1>%', $data, $result, PREG_PATTERN_ORDER);
   print_r($result);
?>
</pre>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.