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
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>

Link to comment
Share on other sites

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

 

Link to comment
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('%<(\w+)[^>]+class="(?:customize|date)">(.*?)</\1>%', $data, $result, PREG_PATTERN_ORDER);
   print_r($result);
?>
</pre>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.