Solved

PHP Find content in HTML source.

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Creating a slider 12 35
Tracking Bouncebacks in PHPMailer 3 11
Can't connect to my database 2 15
Change password through link 1 13
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

13 Experts available now in Live!

Get 1:1 Help Now