Solved

PHP Find content in HTML source.

Posted on 2012-03-23
3
357 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 109

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 109

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

The purpose of this article is to demonstrate how we can use conditional statements using Python.
This article discusses how to create an extensible mechanism for linked drop downs.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

815 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

12 Experts available now in Live!

Get 1:1 Help Now