DIGIT-ARC read-only archive · snapshot: July 23, 2026

PHP Help me extract page number from text

13 posts · originally from https://geek.digit.in/community/threads/php-help-me-extract-page-number-from-text.158471/

bukaida
bukaida Original post #1
Jun 13, 2012 at 12:01 PM
The document is inserted as a text file in the database. the table structure is

id(number) Title(Text) Body(Long Text)

The body may be a 500 page book in text format.

The content in the text file is written (continuously) as

-(-p- 1 )-

content of page 1

-(-p- 2 )-

content of page 2

---so on

Now I need to store this data as individual page in another table automatically which has the following structure--

id ( may be FK from previous table)
content (page wise text content, automatically extracted)
page number( automatically extracted)

I am in total blank how to generate the second table from the first one. I am using PHP 5 with Mysql 5 and Apache 2.2.
Any suggestion is gracefully accepted.

P.S-- The actual page numbers are unicode non-english numbers which are unique and never repeated inside the body of the text.
cute.bandar
Jun 13, 2012 at 1:46 PM
maybe have a look at querypath OR preg_match OR simple_html_dom ?

I think, but am not sure, that using preg_match would solve your issue. Check its manual page and report back if you need more help..
bukaida
bukaida #3
Jun 13, 2012 at 11:12 PM
maybe have a look at querypath OR preg_match OR simple_html_dom ?

I think, but am not sure, that using preg_match would solve your issue. Check its manual page and report back if you need more help..

I have to extract the text between the two page numbers and assign the smaller page number (between the two) to the extracted page and insert them to the table immediately. This process continues until the end of the file is reached. I cannot use a counter to generate page number as the book contains index pages and references also which does not have any page number.

P.S- I think querypath cannot be used with plain text, it can be used only with html or XML. Not sure though.
cute.bandar
Jun 13, 2012 at 11:24 PM
hmm
then maybe you could look for double/triple linebreaks or something. Just thinking... Its difficult for me to say conclusively without seeing a sample of the data.
nbaztec
nbaztec #5
Jun 14, 2012 at 11:20 AM
Use
PHP:
      $pages = preg_split('/-(-p- \d+ )-/', $body);
      foreach($pages as $i=>$p)
           echo "Page no: $i", "Body: $p";
khmadhu
khmadhu #6
Jun 14, 2012 at 5:45 PM
^
@nbaztec you need to escape '(' and '-' characters.

so
PHP:
 /-(-p- \d+ )-/
becomes
PHP:
   /\-\(\-p\- \d+ \)\-/
nbaztec
nbaztec #7
Jun 14, 2012 at 6:02 PM
^
@nbaztec you need to escape '(' and '-' characters.

so
PHP:
 /-(-p- \d+ )-/
becomes
PHP:
   /\-\(\-p\- \d+ \)\-/

No only the '(' which I missed since I copied OP's string directly.
khmadhu
khmadhu #8
Jun 15, 2012 at 1:49 PM
I thought '-' may interfere with range symbol, I think it depends on regex engine!.
nbaztec
nbaztec #9
Jun 15, 2012 at 4:51 PM
No, '-' has meaning only inside character classes. It's valid across all common regex engines.
bukaida
bukaida #10
Jun 16, 2012 at 10:54 AM
Ok guys, here is the sample data
Code:
-(-পৃ- ৩৭৮ )-

গোরা


-(-পৃ- ৩৭৯)-
১  
শ্রাবণ মাসের সকালবেলায় মেঘ কাটিয়া গিয়া নির্মল রৌদ্রে কলিকাতার আকাশ ভরিয়া গিয়াছে। রাস্তায় গাড়িঘোড়ার বিরাম নাই, ফেরিওয়ালা অবিশ্রাম হাঁকিয়া চলিয়াছে, যাহারা আপিসে কালেজে আদালতে যাইবে তাহাদের জন্য বাসায় বাসায় মাছ-তরকারির চুপড়ি আসিয়াছে ও রান্নাঘরে উনান জ্বালাইবার ধোঁওয়া উঠিয়াছে কিন্তু তবু এত বড়ো এই-যে কাজের শহর কঠিনহৃদয় কলিকাতা, ইহার শত শত রাস্তা এবং গলির ভিতরে সোনার আলোকের ধারা আজ যেন একটা অপূর্ব যৌবনের প্রবাহ বহিয়া লইয়া চলিয়াছে। 
এমন দিনে বিনা-কাজের অবকাশে বিনয়ভূষণ তাহার বাসার দোতলার বারান্দায় একলা দাঁড়াইয়া রাস্তায় জনতার চলাচল দেখিতেছিল। কালেজের পড়াও অনেক দিন চুকিয়া গেছে, অথচ সংসারের মধ্যেও প্রবেশ করে নাই, বিনয়ের অবস্থাটা এইরূপ। সভাসমিতি চালানো এবং খবরের কাগজ লেখায় মন দিয়াছে— কিন্তু তাহাতে সব মনটা ভরিয়া উঠে নাই। অন্তত আজ সকালবেলায় কী করিবে তাহা ভাবিয়া না পাইয়া তাহার মনটা চঞ্চল হইয়া উঠিতেছিল। পাশের বাড়ির ছাতের উপরে গোটা-তিনেক কাক কী লইয়া ডাকাডাকি করিতেছিল এবং চড়ুই-দম্পতি তাহার বারান্দার এক কোণে বাসা-নির্মাণ-ব্যাপারে পরস্পরকে কিচিমিচি শব্দে উত্সাহ দিতেছিল— সেই-সমস্ত অব্যক্ত কাকলি বিনয়ের ইচ্ছা করিতে লাগিল বাউলকে ডাকিয়া এই অচিন পাখির গানটা লিখিয়া লয়, কিন্তু ভোর-রাত্রে যেমন শীত-শীত করে অথচ গায়ের কাপড়টা টানিয়া লইতে উদ্যম থাকে না, তেমনি একটা আলস্যের ভাবে বাউলকে ডাকা হইল না, গান লেখাও হইল না, কেবল ঐ অচেনা পাখির সুরটা মনের মধ্যে গুন্* গুন্* করিতে লাগিল। 
বিনয় তাঁহাকে ধরাধরি করিয়া নামাইয়া দিল, এবং তাঁহার মুখ বিবর্ণ হইয়া গেছে দেখিয়া জিজ্ঞাসা করিল, “আপনার লাগে নি তো?” 
তিনি “না, কিছু হয় নি” বলিয়া হাসিবার চেষ্টা করিলেন, সে হাসি তখনই মিলাইয়া গেল এবং তিনি মূর্ছিত হইয়া পড়িবার উপক্রম করিলেন। বিনয় তাঁহাকে ধরিয়া ফেলিল ও উত্কণ্ঠিত মেয়েটিকে কহিল, “এই সামনেই আমার বাড়ি ; ভিতরে চলুন।” 
বৃদ্ধকে বিছানায় শোওয়ানো হইলে মেয়েটি চারি দিকে তাকাইয়া দেখিল ঘরের কোণে একটি জলের কুঁজা আছে। তখনই সেই কুঁজার জল গেলাসে করিয়া লইয়া বৃদ্ধের মুখে ছিটা দিয়া বাতাস 
-(-পৃ- ৩৮০)-

Donot bother much about the content ( A text in bengali from tagore, you can copy-paste to google translate if you are really interested :) )

The interesting terms are -(-পৃ- ৩৭৮ )- and -(-পৃ- ৩৭৯)- from where only the ৩৭৮ and ৩৭৯ are page numbers. The aim is to extract the content between this two and assign ৩৭৮ to it since it appeared earlier ( cannot do comparison to find the smaller directly, I think).
nbaztec
nbaztec #11
Jun 16, 2012 at 11:51 AM
The thing is regex engines don't support UTF-8. But they support Unicode. So you'll have to form the regex manually while knowing the unicode codes for the characters.

For eg:

PHP:
$pages = preg_split('/-\s*\(\s*-\s*\x{09aa}\x{09c3}\s*-\s*([\x{09e6}-\x{09ef}]+)\s*\)-\)-/u', $s));

The regex can be used to split on those lines and also to obtain the page numbers. Alternatively the indexes of the pages are themselves page numbers.
bukaida
bukaida #12
Jun 17, 2012 at 2:17 PM
The thing is regex engines don't support UTF-8. But they support Unicode.
Alternatively the indexes of the pages are themselves page numbers.

The original document is in unicode only. However, since mysql supports utf8 not unicode, I had to convert it to utf8.

The pages are not continuous. So index cannot be used.
nbaztec
nbaztec #13
Jun 17, 2012 at 2:40 PM
The original document is in unicode only. However, since mysql supports utf8 not unicode, I had to convert it to utf8.

The pages are not continuous. So index cannot be used.

Then finding out the unicode values shouldn't be a problem for you. As for the page numbers, the same expression can obtain the page number through preg_match() and will be group 1 or $match[1].