Solved

PHP Find content in HTML source.

Posted on 2012-03-23
3
359 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 110

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 110

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

710 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