Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP Find content in HTML source.

Posted on 2012-03-23
3
Medium Priority
?
370 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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Suggested Courses

824 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