Page 1 of 1

string delete

Posted: Wed Nov 10, 2010 4:25 am
by php learner
Hi,

I am new to php and need help over very simple script.

$strText = file_get_contents( 'ww.google.com/file.php' );

$strFind = 'word';

$blMatch = false;

if( preg_match( "/$strFind/", $strText ) ) {

$blMatch = true;

$strText = str_replace( $strFind, '', $strText );

}

now in text file content are like this

"word is very usefull and have meaning full things."

Now I want that if the script has find "word is very usefull" it should remove the whole line, not only that particular word.


Anyone can help me in this case?

Re: string delete

Posted: Wed Nov 10, 2010 5:04 am
by cpetercarter
You can explode() $strText on the new-line character (\n) to create an array of lines; go through the array and remove the lines that contain "word"; and then stick the lines back together again. Like this:

Code: Select all

$strText = file_get_contents( 'www.google.com/file.php' );
$strFind = 'word';
$lines = explode('\n', $strText);
$blMatch = false;
foreach ($lines as $key => $line)
{
    if strpos($line, $strFind)
    {
        unset ($lines[$key]);
        $sblMatch = true;
    }
}
$strText_edited = implode($lines, '\n');

Re: string delete

Posted: Wed Nov 10, 2010 5:24 am
by php learner
Hi

Basically I want to open a page in webbrowser which is

http://www.google.com/file.php

now, i want that there is some text in which i want to remove some text for that this one i think help

Code: Select all

$strText = file_get_contents( 'www.google.com/file.php' );
$strFind = 'word';
$lines = explode('\n', $strText);
$blMatch = false;
foreach ($lines as $key => $line)
{
    if strpos($line, $strFind)
    {
        unset ($lines[$key]);
        $sblMatch = true;
    }
}
$strText_edited = implode($lines, '\n');
Now how to actually echo to that browser with deleted text I am having the issue.

Could you give me some idea how to actually do that?

Re: string delete

Posted: Wed Nov 10, 2010 8:29 am
by cpetercarter
I think I may have misunderstood what you want to achieve.

I assumed that file.php simply output text, with each item on a new line. If that is the case, then the solution I suggested in my previous post will work and you can send the amended file $strText_edited to a browser with a simple echo statement.

However, if file.php outputs a webpage, with html, images, javascript etc, then your task is much more difficult. You would need some complex manipulation of the DOM to remove the lines you want. Then, if you want to send the text to a browser from your server, you would hit the problem that any relative urls in the output (eg urls of javascript files, images, css files) would not work. In short, it is not easy to take someone else's web-page, edit it on the fly, and then output it to a browser. Unless you really understand how the web-page output by file.php works, I would not even try.