Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Php Replace Help!
Tom743
post Apr 13 2008, 02:30 PM
Post #1


Newbie
*

Group: Members
Posts: 7
Joined: 12-January 08
Member No.: 56,132



CODE
<?php
function code( $input ){

$data[0] = "<?php include(\"shoutbox.php?no=";
$data[1] = "\"); ?>";

$find[0] = "/\[b\](.*?)\[\/b\]/i";
$find[1] = "/\[url\=(.*)\](.*)\[\/url\]/";
$find[2] = "/\[u\](.*?)\[\/u\]/i";
$find[3] = "/\[i\](.*?)\[\/i\]/i";
$find[4] = "/\[big\](.*?)\[\/big\]/i";
$find[5] = "/\[small\](.*?)\[\/small\]/i";
$find[6] = "/\[shoutbox]id=(.*)\[\/shoutbox\]/";

$replace[0] = "<b>$1</b>";
$replace[1] = "<a href=\"$1\">$2</a>";
$replace[2] = "<u>$1</u>";
$replace[3] = "<i>$1</i>";
$replace[4] = "<font size=\"5\">$1</font>";
$replace[5] = "<font size=\"2\">$1</font>";
$replace[6] = "$data[0]$1$data[1]";

$change = preg_replace($find, $replace, $input);
print $change;
}
code("[shoutbox]id=1[/shoutbox]");
?>


In my code, the shoutbox doesnt work. The code is ment to replace [shoutbox]no= with <?php include("shoutbox.php?no= then replace [/shoutbox] with "); ?> which it does, but then the php code just appears in the sorce of the webpage. Does anyone know how to fix it?

Thanks

Notice from electriic ink:
There's no need to sign your name. We can see who started the topic smile.gif


This post has been edited by electriic ink: Apr 13 2008, 02:57 PM
Go to the top of the page
 
+Quote Post
electriic ink
post Apr 13 2008, 03:19 PM
Post #2


Incest is a game the whole family can play.
Group Icon

Group: [MODERATOR]
Posts: 1,205
Joined: 11-February 05
From: Heaven
Member No.: 3,709



Wouldn't it be easier just to do this?

CODE
$string = '[shoutbox]id=1[/shoutbox]';
$string = str_replace ('[shoutbox]id=', '', $string);
$string = str_replace ('[/shoutbox]', '', $string);

include ('shoutbox.php?no=' . $string);


Using that you could create a seperate function:

CODE
function shoutbox_tag ($input) {

$string = strstr ($input, '[shoutbox]');    // Removes all parts of string that are NOT [shoutbox]...[/shoutbox]
$string = strstr ($string, '[/shoutbox]', true);
$string = $string . '[/shoutbox]'; // End remove

$string = str_replace ('[shoutbox]id=', '', $string);
$string = str_replace ('[/shoutbox]', '', $string);

return $string;

}


Which you would call as follows:

CODE
include ('shoutbox.php?no=' . shoutbox_tag('123123[shoutbox]id=1[/shoutbox]123123'));
Go to the top of the page
 
+Quote Post
truefusion
post Apr 13 2008, 09:42 PM
Post #3


Ephesians 6:10-17
Group Icon

Group: [MODERATOR]
Posts: 1,868
Joined: 22-June 05
From: The World of Gentoo
Member No.: 8,528
T17 GFX Crew



If you want to execute a string as if it were PHP code, consider researching eval. Just be careful with this function, as you wouldn't want user input to be ran as PHP code, so you may need to properly verify the string before running eval.

BTW, you can't include files the way you want them to be included; that is, assigning _GET variables.
Go to the top of the page
 
+Quote Post
Tom743
post Apr 15 2008, 04:17 PM
Post #4


Newbie
*

Group: Members
Posts: 7
Joined: 12-January 08
Member No.: 56,132



Is it possible when using PHP preg_replace, to include something, rather than displaying text. The code is below, the proplem is with the shoutbox.

CODE
<?php
function code( $input ){

$find[0] = "/\[b\](.*?)\[\/b\]/i";
$find[1] = "/\[url\=(.*)\](.*)\[\/url\]/";
$find[2] = "/\[u\](.*?)\[\/u\]/i";
$find[3] = "/\[i\](.*?)\[\/i\]/i";
$find[4] = "/\[big\](.*?)\[\/big\]/i";
$find[5] = "/\[small\](.*?)\[\/small\]/i";
$find[6] = "/\[shoutbox]id=(.*?)\[\/shoutbox\]/";

$replace[0] = "<b>$1</b>";
$replace[1] = "<a href=\"$1\">$2</a>";
$replace[2] = "<u>$1</u>";
$replace[3] = "<i>$1</i>";
$replace[4] = "<font size=\"5\">$1</font>";
$replace[5] = "<font size=\"2\">$1</font>";
$replace[6] = include("shoutbox.php?no=$1");
$change = preg_replace($find, $replace, $input);
echo $change;
}
code("[shoutbox]id=2[/shoutbox]");
?>


It doesnt replace the $1 with the shoutbox id and the error message is;


QUOTE
[b]Warning: include(shoutbox.php?no=$1) [function.include]: failed to open stream: No error in C:\wamp\www\replace\index.php on line 18

Warning: include() [function.include]: Failed opening 'shoutbox.php?no=$1' for inclusion (include_path='.;C:\php5\pear') in C:\wamp\www\replace\index.php on line 18

Notice from jlhaslip:
Moved to PHP Programming

Notice from truefusion:
Merging topics
Go to the top of the page
 
+Quote Post
truefusion
post Apr 15 2008, 06:41 PM
Post #5


Ephesians 6:10-17
Group Icon

Group: [MODERATOR]
Posts: 1,868
Joined: 22-June 05
From: The World of Gentoo
Member No.: 8,528
T17 GFX Crew



QUOTE(Tom743 @ Apr 15 2008, 12:17 PM) *
It doesnt replace the $1 with the shoutbox id

Even if it did replace the 1 variable, it still wouldn't work. As i've mentioned above, you cannot include files the way you want to. If you want to set $_GET['id'], you have to do so with the parent page.
Go to the top of the page
 
+Quote Post
Tom743
post Apr 15 2008, 08:28 PM
Post #6


Newbie
*

Group: Members
Posts: 7
Joined: 12-January 08
Member No.: 56,132



QUOTE(electriic ink @ Apr 13 2008, 04:19 PM) *
Wouldn't it be easier just to do this?

CODE
$string = '[shoutbox]id=1[/shoutbox]';
$string = str_replace ('[shoutbox]id=', '', $string);
$string = str_replace ('[/shoutbox]', '', $string);

include ('shoutbox.php?no=' . $string);


Using that you could create a seperate function:

CODE
function shoutbox_tag ($input) {

$string = strstr ($input, '[shoutbox]');    // Removes all parts of string that are NOT [shoutbox]...[/shoutbox]
$string = strstr ($string, '[/shoutbox]', true);
$string = $string . '[/shoutbox]'; // End remove

$string = str_replace ('[shoutbox]id=', '', $string);
$string = str_replace ('[/shoutbox]', '', $string);

return $string;

}


Which you would call as follows:

CODE
include ('shoutbox.php?no=' . shoutbox_tag('123123[shoutbox]id=1[/shoutbox]123123'));

Ok, but if im not just going to add a shoutbox code in there, i will post other tags like so then it wont work sad.gif
Go to the top of the page
 
+Quote Post
electriic ink
post Apr 18 2008, 07:10 PM
Post #7


Incest is a game the whole family can play.
Group Icon

Group: [MODERATOR]
Posts: 1,205
Joined: 11-February 05
From: Heaven
Member No.: 3,709



Sorry for neglecting your topic smile.gif

QUOTE(Tom743)
Ok, but if im not just going to add a shoutbox code in there, i will post other tags like so then it wont work


It does. That's what this part of the code does:

CODE
$string = strstr ($input, '[shoutbox]');    // Removes all parts of string that are NOT [shoutbox]...[/shoutbox]
$string = strstr ($string, '[/shoutbox]', true);
$string = $string . '[/shoutbox]'; // End remove


It removes all parts of input that are not [shoutbox]...[/shoutbox] and processes them separately. Unless you can see otherwise...

BTW, what truefusion is saying about including files (I don't think you understand - sorry if you do) is this:

You can't include a file like this:

CODE
include 'file.php?id=1';


And have the code in file.php as:

CODE
function ($_GET['id']);


It doesn't work. If you want to "give" a variable to an included file, you have to this:

CODE
$id = 1;
include 'file.php';


And have the code in file.php as:

CODE
function ($id);
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Help With Reading Files(5)
  2. Bush Nominate Roberts As Chief Justice(1)
  3. How To Replace Ram In Your Pc(0)
  4. How To Replace A Cd-rom Drive In A Pc(3)
  5. Wmp (windows Media Photo) - The New Image File Format From Microsoft(31)
  6. Replace A Character In A Specific Tag(7)
  7. Help With Preg_replace(3)
  8. Preg_replace Problems(7)
  9. 'red Devils' Replace Blues As Champions(0)
  10. How To Replace Japanese Cute Actor Face With Yours(1)
  11. Php Preg Replace(1)
  12. Unable To Replace .png File In Directory(7)
  13. Preg Replace Problem(1)
  14. A Simple Preg_replace Help Please.(1)