473,394 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Running a regular expression on the HTML of the current page

60
[PHP]if(eregi("something", $xxxxx)){
return true;
}else{
return false;
}[/PHP]

I have that regexp and want to make $xxxxx be the HTML webpage the user is seeing with his browser. For example: the file in which the regexp is, it-s called s.php and it has an html form and lot of other functions.
So i want to refer $xxxxx to the HTML code the user is currently seeing
Aug 27 '07 #1
5 1284
pbmods
5,821 Expert 4TB
Heya, Pplers.

Changed thread title to better describe the problem.

Depending on how you are generating the HTML, you may find it more pragmatic to do something like this:
Expand|Select|Wrap|Line Numbers
  1. $somethingEchoed = false;
  2. if( {condition} )
  3. {
  4.     echo 'something';
  5.     $somethingEchoed = true;
  6. }
  7.  
  8. .
  9. .
  10. .
  11. .
  12.  
  13. // Instead of eregi()ing for 'something'...
  14. return $somethingEchoed;
  15.  
Where is the HTML that you'd like to parse coming from?
Aug 27 '07 #2
pplers
60
from other functions in other pages.
Aug 27 '07 #3
pbmods
5,821 Expert 4TB
Heya, Pplers.

That's tricky. You have a couple of options:
  • You can set the value of a global variable when whatever it is gets output.
  • If this is not an option, you can use output buffering. Call this at the start of your script:
    Expand|Select|Wrap|Line Numbers
    1. ob_start('checkForSomething');
    2.  
    3. function checkForSomething( $str )
    4. {
    5.     if( eregi('something', $str) )
    6.     {
    7.         // Don't just do something, sit there!
    8.     }
    9.  
    10.     return $str;
    11. }
    12.  
    And then at the end of your script, when you want to check for the 'something':
    Expand|Select|Wrap|Line Numbers
    1. ob_end_flush();
    2.  
Aug 27 '07 #4
pplers
60
but how should i call the function with the argument, i mean what should $str be ?
I just want the ereg to return True/False so i am talking what i did above, but how do i call the function ?
Aug 27 '07 #5
pbmods
5,821 Expert 4TB
Heya, Pplers.

Do the functions that generate the HTML return it or echo it?

If they return the HTML, then you can simply use a variable to store the return values of your functions and then echo it when you are done.

If your functions output the HTML, then you'll have to use output buffering.

Note that you never actually explicitly call checkForSomething(). Instead, you pass it as a callback to ob_start(). When you call ob_end_flush(), PHP will automatically call checkForSomething() and pass all of the HTML that it was buffering (everything that your functions echoed) as $str.

Here's a simple example:
Expand|Select|Wrap|Line Numbers
  1. function processOutput($str)
  2. {
  3.     return '<strong>' . $str . '</strong>';
  4. }
  5.  
  6. ob_start('processOutput');
  7.  
  8. echo 'Hello, World!';
  9.  
  10. // Note that NOTHING has been output yet; it's in the output buffer.
  11.  
  12. ob_end_flush();  // Here is where the text gets output.
  13.  
When this code executes, PHP turns on output buffering so that anything we echo, print, etc., gets put into a temporary buffer. When we call ob_start(), we assign a callback to the processOutput() function.

When we echo 'Hello, World!', because output buffering is active, instead of sending the text to the browser, we save the text in the output buffer instead.

Then when we call ob_end_flush(), the callback (processOutput()) gets called, and PHP passes all of the buffered text ('Hello, World!') to it.

processOutput() takes $str ('Hello, World!') and surrounds it with HTML strong tags. Then it returns the resulting string, which ob_end_flush() then sends to the browser.

The end result:
Expand|Select|Wrap|Line Numbers
  1. <strong>Hello, World!</strong>
  2.  
Aug 27 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ori | last post by:
Hi , I'm working with C#.NET and I'm looking for the following. I have a web page content and I want to pull all the text which appear in the page without all the HTML tags. I know that there...
9
by: Mike P | last post by:
I have a regular expression that I use on text boxes where I want to limit the user to letters a-z and spaces. I now need to allow characters such as ö, ä and å (Nordic characters). Does anybody...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
6
by: alexrussell101 | last post by:
For anyone who can't be bothered to read my code and examples, scroll to the bottom, the question's there. Thanks. I'm using php and regular expressions to convert bbcode style things to html....
9
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use...
6
by: Ludwig | last post by:
Hi, i'm using the regular expression \b\w to find the beginning of a word, in my C# application. If the word is 'public', for example, it works. However, if the word is '<public', it does not...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
1
by: coder57 | last post by:
Basically I have a local html file, called file1.html it has a series of links (with a particular domain name) in addition to the html code, I am trying to follow each of these links (based on the...
0
by: peter stickney | last post by:
Excuse the babbling or lack of sense, it's been a long day. I am making a flat file, static HTML search engine for a site. I downloaded a script from the net and have been working with it for my...
2
by: Joey | last post by:
Hello guys, I'm trying to learn about regular expressions. I need to be able to use an RE that can evaluate for STRINGS (or specific sequences of characters), not just occurances of characters....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.