Jump to content


Preg_match_all help...


3 replies to this topic

#1 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 26 September 2006 - 10:05 PM

I'm trying to make a little way of splitting a tutorial into pages after being pulled form a database. So my solution is simple: make comments in the tutorial such as '<!-- PART 1 -->(Content)<!-- /PART 1-->', then using preg_match_all() to grab them and split them, and echo according to the page desired.

Problem is, I can't get preg_match_all to return anything. >.<

Here are a few things I've tried.
preg_match_all("|(<!-- PART) [^>]+ (-->)(.*)(<!-- /PART) [^>]+ (-->)|iU", $tutorial, $tutorial_data);
preg_match_all("|<(!-- PART) [^>]+ (--)>(.*)<(!-- /PART) [^>]+ (--)>|iU", $tutorial, $tutorial_data);
preg_match_all("|<(!--) [^>]+ (--)>(.*)<(!--) /[^>]+ (--)>|iU", $tutorial, $tutorial_data);
preg_match_all('#<\!-- (PART 1) -->(.*)<\!-- (/PART 1) -->#i', $tutorial, $tutorial_data);

And a few other variations.
I've gone basic for debugging, such as only doing the front tag and not the back, specifically putting a number instead of a regex bit for 'any number', etc, but when I use print_r(), this is what I get.
Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) [3] => Array ( ) [4] => Array ( ) [5] => Array ( ) )

Empty arrays. :S

Can anyone help me a little? I just don't understand how it can be returning the fact that there are matches, but does not output any actual match data. I've experimented with this function a tad, and it does return the results, but not with what I'm trying to do. ^_^

And I've also tried using all flags that the function has available, and those only result in less data, such as one empty array, instead of an empty multi-dimensional array. :huh:

#2 BlazeForc3

    Young Padawan

  • Members
  • Pip
  • 62 posts
  • Location:Australia

Posted 27 September 2006 - 12:02 AM

Instead of using a regular expression a much easier way would be to use the explode function.

$content = explode("[NEW_PAGE]", $tutorial); // Split the tutorial into pages
$pages = count($content); // Total Number of Pages

So this is how you would structure your tutorial:

Tutorial Text: Page 1[NEW_PAGE]Tutorial Text: Page 2

$content[0] would refer to page 1 of the tutorial, $content[1] would refer to page 2 and so on, this can get a bit confusing so we will correct it in our final script.

Assuming we take the tutorial contents from our database and store it in the variable $tutorial this is how the code would look:

$content = explode("[NEW_PAGE]", $tutorial); // Split the tutorial into pages
$pages = count($content); // Total Number of Pages

$number = $number - 1; // $number would be parsed through the URL (http://www.url.com/tutorials.php?id=1&number=2) and will refer to the page number we want to show

echo $content[$number]; // Display the content for the specified page

We subtracted 1 from the $number variable to make it less confusing since $content[0] is page 1, so when $number=1 it will actually call $content[0].

I've had some bad experiences with regular expressions so I try not to use them whenever possible.

Edited by BlazeForc3, 27 September 2006 - 12:14 AM.


#3 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 27 September 2006 - 03:02 PM

It appears to me that you don't know what parantheses are used for. They're for capturing sub-patterns, not for putting around every word.
preg_match_all('#<!-- PART ([0-9]+) -->(.*?)<!-- /PART \\1 -->#i', $tutorial, $tutorial_data);
That should return something a little more useful. Of course, the explode thing doesn't sound like a bad idea.

If it were me, i would just make seperate files per page, and throw them into their own directory. Maybe make a generic tutorial.php file that would out put the title/page/section of the tutorial as well as it's contents.
i.e.
/* Output tutorial info... */
$tutorial = file_get_contents('tutorial_name/page'.$_GET['page'].'.txt');
echo $tutorial;
/* Links to next/prev page */

Edited by rc69, 27 September 2006 - 03:03 PM.


#4 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 27 September 2006 - 03:36 PM

Lol rc, that is the exact string I used in one of my first attempts. It didn't give me anything at all...

I'll give it one more go though.
If I don't have any luck I'll try the explode way. I was thinking about using that just as I was walking home, lol.

Edit:
Gave another empty array, but the explode worked just great, thanks Blaze. :(

Edited by Demonslay, 27 September 2006 - 03:59 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users