Author Topic: [SOLVED] preg_match problem  (Read 861 times)

0 Members and 1 Guest are viewing this topic.

Offline ted_chou12Topic starter

  • Devotee
  • Posts: 1,481
  • Gender: Male
  • Hitman Hits
    • View Profile
    • NetFriending
[SOLVED] preg_match problem
« on: November 12, 2008, 12:06:13 PM »
I have a text:
Code: [Select]
<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$resultPREG_PATTERN_ORDER);

Which gets the title, but I cant do the same to get the content, does anyone know how?
Thanks,
Ted.
Currently working on a website called NetFriending.co.cc, it is a free social networking site, features such as guestbook, blogs, are fully customizable in NetFriending, also has a forum to help you get to know others, if you are interested in the site, the url is: NetFriending.co.cc Hope everyone can get involved because the site is still quite new, so not much people are there at the moment, please feel free to leave a comment there or send a message to our staff. Register Here The link above is for registeration, it is 100% Free and Safe, everyone there is friendly and you can build your network. Photo sharing upload your own photos as well!!!

Offline effigy

  • Staff Alumni
  • Freak!
  • *
  • Posts: 7,301
  • Gender: Male
  • We must be the change we wish to see in the world.
    • View Profile
Re: preg_match problem
« Reply #1 on: November 12, 2008, 12:58:27 PM »
Code: [Select]
<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$resultPREG_PATTERN_ORDER);
print_r($result);
?>

</pre>
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

Offline ted_chou12Topic starter

  • Devotee
  • Posts: 1,481
  • Gender: Male
  • Hitman Hits
    • View Profile
    • NetFriending
Re: preg_match problem
« Reply #2 on: November 14, 2008, 03:10:06 AM »
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:
Code: [Select]
<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
Currently working on a website called NetFriending.co.cc, it is a free social networking site, features such as guestbook, blogs, are fully customizable in NetFriending, also has a forum to help you get to know others, if you are interested in the site, the url is: NetFriending.co.cc Hope everyone can get involved because the site is still quite new, so not much people are there at the moment, please feel free to leave a comment there or send a message to our staff. Register Here The link above is for registeration, it is 100% Free and Safe, everyone there is friendly and you can build your network. Photo sharing upload your own photos as well!!!

Offline ted_chou12Topic starter

  • Devotee
  • Posts: 1,481
  • Gender: Male
  • Hitman Hits
    • View Profile
    • NetFriending
Re: preg_match problem
« Reply #3 on: November 14, 2008, 03:13:41 AM »
I came up with some random codes

preg_match_all
('/\sclass="(?:customize|date)">(.*)?<\/(p|span)>/'$content$array1PREG_PATTERN_ORDER);

hope it gives some ideas,
 :P
Currently working on a website called NetFriending.co.cc, it is a free social networking site, features such as guestbook, blogs, are fully customizable in NetFriending, also has a forum to help you get to know others, if you are interested in the site, the url is: NetFriending.co.cc Hope everyone can get involved because the site is still quite new, so not much people are there at the moment, please feel free to leave a comment there or send a message to our staff. Register Here The link above is for registeration, it is 100% Free and Safe, everyone there is friendly and you can build your network. Photo sharing upload your own photos as well!!!

Offline effigy

  • Staff Alumni
  • Freak!
  • *
  • Posts: 7,301
  • Gender: Male
  • We must be the change we wish to see in the world.
    • View Profile
Re: preg_match problem
« Reply #4 on: November 14, 2008, 10:05:20 AM »
Code: [Select]
<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$resultPREG_PATTERN_ORDER);
   
print_r($result);
?>

</pre>
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

Offline ted_chou12Topic starter

  • Devotee
  • Posts: 1,481
  • Gender: Male
  • Hitman Hits
    • View Profile
    • NetFriending
Re: preg_match problem
« Reply #5 on: November 14, 2008, 10:54:04 AM »
Hi, I got this error:
Warning: preg_match_all() [function.preg-match-all]: Unknown modifier '\' in /home/user/public_html/blog_feed.php on line 29
when I was trying to correct it.
Thanks,
Ted
Currently working on a website called NetFriending.co.cc, it is a free social networking site, features such as guestbook, blogs, are fully customizable in NetFriending, also has a forum to help you get to know others, if you are interested in the site, the url is: NetFriending.co.cc Hope everyone can get involved because the site is still quite new, so not much people are there at the moment, please feel free to leave a comment there or send a message to our staff. Register Here The link above is for registeration, it is 100% Free and Safe, everyone there is friendly and you can build your network. Photo sharing upload your own photos as well!!!

Offline ted_chou12Topic starter

  • Devotee
  • Posts: 1,481
  • Gender: Male
  • Hitman Hits
    • View Profile
    • NetFriending
Re: preg_match problem
« Reply #6 on: November 15, 2008, 10:43:18 AM »
Hello, my bad, there was no error, I think I accidentally deleted something in there, but that expression gives span in the </span> tag, I dont understand  ???
Currently working on a website called NetFriending.co.cc, it is a free social networking site, features such as guestbook, blogs, are fully customizable in NetFriending, also has a forum to help you get to know others, if you are interested in the site, the url is: NetFriending.co.cc Hope everyone can get involved because the site is still quite new, so not much people are there at the moment, please feel free to leave a comment there or send a message to our staff. Register Here The link above is for registeration, it is 100% Free and Safe, everyone there is friendly and you can build your network. Photo sharing upload your own photos as well!!!

Offline ted_chou12Topic starter

  • Devotee
  • Posts: 1,481
  • Gender: Male
  • Hitman Hits
    • View Profile
    • NetFriending
Re: [SOLVED] preg_match problem
« Reply #7 on: November 17, 2008, 10:25:58 AM »
I got it~ :)

preg_match_all
("/<(?:p|span|h3)\sclass=\"(?:customize|date)\">(.*?)<\/(?:p|span|h3)>/",  $content$array1PREG_PATTERN_ORDER);

Ted
Currently working on a website called NetFriending.co.cc, it is a free social networking site, features such as guestbook, blogs, are fully customizable in NetFriending, also has a forum to help you get to know others, if you are interested in the site, the url is: NetFriending.co.cc Hope everyone can get involved because the site is still quite new, so not much people are there at the moment, please feel free to leave a comment there or send a message to our staff. Register Here The link above is for registeration, it is 100% Free and Safe, everyone there is friendly and you can build your network. Photo sharing upload your own photos as well!!!