string delete

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
php learner
Forum Newbie
Posts: 2
Joined: Wed Nov 10, 2010 4:24 am

string delete

Post 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?
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: string delete

Post 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');
php learner
Forum Newbie
Posts: 2
Joined: Wed Nov 10, 2010 4:24 am

Re: string delete

Post 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?
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: string delete

Post 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.
Post Reply