PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Nov 22, 2019 9:00 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Sun May 08, 2005 7:42 pm 
Offline
Forum Newbie

Joined: Sun May 08, 2005 7:28 pm
Posts: 2


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 08, 2005 8:28 pm 
Offline
Forum Contributor

Joined: Tue Jun 24, 2003 7:33 pm
Posts: 181
You could use str_replace().

Just open a file, put its contents into a variable and use str_replace() to replace a string with the desired string.

Syntax: [ Download ] [ Hide ]
$file_open = fopen($file, 'r');

$file_read = fread($file_open, filesize($file)); //Contents of $file



str_replace($to_replace, $replace_with, $file_read);


Give that a whirl. The contents of $file_read doesn't have to be a file. It can be any type of string or array.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 08, 2005 10:18 pm 
Offline
Site Admin
User avatar

Joined: Tue Dec 23, 2003 3:10 am
Posts: 11470
Location: Toronto
or you can even minimize that with

Syntax: [ Download ] [ Hide ]
str_replace($to_replace, $replace_with, file_get_contents($filename));


I would recommend using file_get_contents() instead of fopen then fread


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 09, 2005 12:04 am 
Offline
Forum Newbie

Joined: Sun May 08, 2005 7:28 pm
Posts: 2


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 09, 2005 12:27 am 
Offline
Forum Contributor

Joined: Tue Jun 24, 2003 7:33 pm
Posts: 181


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 09, 2005 4:35 am 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 09, 2005 4:44 am 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia
Here's a nice little trick ;-)

Syntax: [ Download ] [ Hide ]
function getBlock($source, $tag) {

    $re = '#<\s*'.$tag.'[^>]*>(.*?)<\s*/\s*'.$tag.'\s*>#is';

    if (preg_match($re, $source, $matches)) {

        $block = $matches[1]; //The bit you need

        return $block;

    } else {

        return false;

    }

}



/*** EXAMPLE ****/

$google_source = file_get_contents('http://www.google.com/');



$googles_body = getBlock($google_source, 'body');



echo '<pre>';

echo htmlspecialchars($googles_body);

echo '</pre>';


Change preg_match() to preg_match_all() if you're looking for numerous items (e.g. &lt;b&gt;text&lt;/b&gt; tags)...

Hope that helps ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 09, 2005 5:36 am 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia
Misunderstood question... apologies.

To avoid replacing HTML tags like you suggested try this...

It will have to be a regex for this anyway.

Untested (can you let me know how this goes please - curious on this one but too busy to test).

Syntax: [ Download ] [ Hide ]
function highlightWords ($source, $word, $color) {

    $re = '#(?<!<)(\s*'.$word.')(?![^>]*>)#is';

    $replace = '<span style="background-color:'.$color.'">$1</span>';

    $highlighted = preg_replace($re, $replace, $source);

    return $highlighted;

}



/*** EXAMPLE ***/

$regex_info_source = file_get_contents('http://www.regular-expressions.info/');

$re_highlighted = highlightWords($regex_info_source, 'regex', '#FFEE00');

echo $re_highlighted;


At first sight the regular expressions looks quite scary (and it unavoidably matches the whitespace preceding the word but nobody sees that).

The (?&lt;!...) is a negative lookbehind (in other words the word must NOT follow "&lt;". Equally the (?!...) is a negative lookahead (in other words the word must not come before "&gt;". The \s* and [^&gt;]* just allow other permittable characters to be in the source code and not cause a problem.

Good luck ;-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group