...

View Full Version : Replacing select text in strings?



Raraken
03-23-2004, 12:36 AM
Is there a way I can have php sort though text, and replace any matching text blurbs, with another text blurb?

Ie replace any [tagsLikeThis]

With <this><this></and></this>

Nightfire
03-23-2004, 12:56 AM
Can do something like this


<?php
$str = "weeeeee";

$tags[0] = "/[tag]/";
$tags[1] = "/[\/tag]/";

$replaced[0] = "<b>";
$replaced[1] = "<\/b>";

echo preg_replace($tags,$replaced,$str);
?>

Raraken
03-23-2004, 01:12 AM
Originally posted by Nightfire
Can do something like this


<?php
$str = "weeeeee";

$tags[0] = "/[tag]/";
$tags[1] = "/[\/tag]/";

$replaced[0] = "<b>";
$replaced[1] = "<\/b>";

echo preg_replace($tags,$replaced,$str);
?>


Thank you!

So, if I ran an entire imported html doc through as $str, would it find and replace every [tag] like that?

Nightfire
03-23-2004, 01:16 AM
it should do, but I've probably just put my foot in it now :p Too tired to think lol

Raraken
03-23-2004, 01:18 AM
Originally posted by Raraken
Thank you!

So, if I ran an entire imported html doc through as $str, would it find and replace every [tag] like that?

Thank you for the help! :thumbsup:

Raraken
03-23-2004, 03:00 AM
Okay... Thers a problem.


<?php
$str = {include("document.htm")};

$tags[0] = "//";
$tags[1] = "//";

$replaced[0] = "<hr color=\"green\">";
$replaced[1] = "<hr color=\"black\">";

echo preg_replace($tags,$replaced,$str[-1]);
?>

That's my code.

I'm trying to convert document.htm (The link works) into a string.

But it says theres a syntax error on line 2 ($str = ... ... tm"]};), and when I take it away so its correct grammar, it say's that the function is wrong, and the reason is "t"

Argh...:confused:

firepages
03-23-2004, 03:36 AM
you can't assign an include like that (without output buffering etc)



<?php
$str = file_get_contents("document.htm") ;
?>
or for older php versions
<?
$str = implode( '' , file("document.htm") ) ;
?>

Raraken
03-23-2004, 10:21 PM
In the script:


<?php
$str = file_get_contents("document.htm");

$tags[0] = "//";
$tags[1] = "//";

$replaced[0] = "_";
$replaced[1] = "<hr color=\"black\">";

echo preg_replace($tags,$replaced,$str);
?>

when it runs, I get this:

Warning: Unknown modifier 't' in /home/sites/site5/web/hypertestfolder/hypercodetest.php on line 10
<_i_le>Un_i_led Documen_ hyper_____op HELLO!

I replaced the pervious HR codes in the first replacement array, with "_" codes. so I could more easily see what was happening.

Basically, it went though and found the LETTERS in the tag section, instead of looking for the whole quote.

So instead of finding "[tag]" it looks for: [, t, a, g, /, and ].

Is there a way to correct this?

Nightfire
03-23-2004, 10:26 PM
Sorry, I should've checked. The forum's stripped my backslashes out my post.



<?php
$str = file_get_contents("document.htm");

$tags[0] = "/\\[tag]/";
$tags[1] = "/\\[\\/tag]/";

$replaced[0] = "_";
$replaced[1] = "<hr color=\"black\">";

echo preg_replace($tags,$replaced,$str);
?>


That'll work now



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum