Solved

PHP Find content in HTML source.

Posted on 2012-03-23
3
354 Views
Last Modified: 2012-06-27
Hi there, I would like to know, how do I find this: (IN HTML SOURCE)

"Now Viewing Page 1 of X" and insert X into the $lastpage .

Example:
"Now Viewing Page 1 of 7"
$lastpage = 7;
0
Comment
Question by:mropenmind
  • 2
3 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37759478
Please post the HTML source.  You can probably use strpos() or explode() to find the data, but it will be better to use a good test data set and be sure of the answer.
0
 

Author Comment

by:mropenmind
ID: 37759486
<div class="pagecontainer">                  
                        <div class="pagelocal"><b>Now Viewing Page 1 of 7</b></div>
                              <div>
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37759500
See http://www.laprbass.com/RAY_temp_mropenmind.php
<?php // RAY_temp_mropenmind.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA FROM THE POST AT EE
$htm = <<<HTM
<div class="pagecontainer">
                        <div class="pagelocal"><b>Now Viewing Page 1 of 7</b></div>
                              <div>
HTM;

// A FUNCTION TO FIND THE LAST PAGE NUMBER
function findLastPage($htm, $sig='Now Viewing Page')
{
    // EXPLODE ON THE SIGNAL STRING
    $arr = explode($sig, $htm);

    // LOCATE THE END OF THE TARGET STRING
    $poz = strpos($arr[1], '<');
    $txt = trim(substr($arr[1],0,$poz));

    // EXPLODE ON THE 'of' STRING
    $arr = explode('of', $txt);
    return trim($arr[1]);
}

$lastpage = FindLastPage($htm);
var_dump($lastpage);

Open in new window

You can probably do this with some kind of regular expression, but I often find that the simplicity of using explode and related string functions makes my code easier to write, test and understand.  HTH, ~Ray
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now