Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace 1

Status
Not open for further replies.

iranor

Programmer
Joined
Jun 17, 2004
Messages
174
Location
CA
$message = "text text [playerinfo]Neomaster[/playerinfo] text etc...";
$string = ('/\[playerinfo\](.*)\[\/playerinfo\]/');

$usercheck = preg_replace($string,"$1",$message);
$username = $usercheck;

How can I change this to make $username = Neomaster?

I need to check the message to see if someone use [playerinfo] [/playerinfo] , Then I need to put what is in the middle of [playerinfo] [/playerinfo] in a variable named $username.

The problem of the above code is that it returns the whole message , not only the username...
 
I recommend you not use preg_replace for this. Rather, use preg_match().

This script:
Code:
<?php
$a = 'text text [playerinfo]Neomaster[/playerinfo] text etc...';

preg_match ('/\[playerinfo\]([^\[]*)\[\/playerinfo\]/', $a, $matches);

print_r ($matches);
?>

Returns the following:
Code:
Array
(
    [0] => [playerinfo]Neomaster[/playerinfo]
    [1] => Neomaster
)



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks! this is perfect. Here's a star
 
$message = "bla bla bla [playerinfo]Neomaster[/playerinfo] second player [playerinfo]Seconduser[/playerinfo]";

//START OF PART
preg_match ('/\[playerinfo\]([^\[]*)\[\/playerinfo\]/', $message, $matches);
$username = $matches[1];
$getplayerinfo = mysql_fetch_array(mysql_query("SELECT * FROM players WHERE username='$username'"));
$message=str_replace("[playerinfo]","
<table width=90% cellspacing=1 cellpadding=3 border=0 align=center>
<tr>
<td><span class=genmed><b> $username:</b></span></td>
</tr>
<tr>
<td class=quote>
Alliance: ".$getplayerinfo['alliance']."<br>
Points: ".$getplayerinfo['points']."<br>
Position: X: ".$getplayerinfo['posx'].", Y: ".$getplayerinfo['posy']."<br>
Description: ".$getplayerinfo['desc']."<br>
</td>
</tr>
</table>
",$message);
$message=str_replace("[/playerinfo]","",$message);
//END OF PART

echo "$message";

Here it is supposed to print 2 table displaying information about the two players.

I want to make a modification to a phpbb forum...

How can I make to allow unlimited use of [playerinfo] [/playerinfo] in a post , and show correct information about them?

Like if user type [playerinfo]Neomaster[/playerinfo] , this should show a table displaying some information about the player.
 
Now I have this :

$message = "bla bla bla [playerinfo]Neomaster[/playerinfo] second player [playerinfo]Seconduser[/playerinfo]";

//START OF PART
preg_match_all ('/\[playerinfo\]([^\[]*)\[\/playerinfo\]/', $message, $matches);
foreach($matches[1] as $username){
$getplayerinfo = mysql_fetch_array(mysql_query("SELECT * FROM players WHERE username='$username'"));
$message=str_replace("[playerinfo]","
<table width=90% cellspacing=1 cellpadding=3 border=0 align=center>
<tr>
<td><span class=genmed><b> $username:</b></span></td>
</tr>
<tr>
<td class=quote>
Alliance: ".$getplayerinfo['alliance']."<br>
Pointage: ".$getplayerinfo['points']."<br>
Position: X: ".$getplayerinfo['posx'].", Y: ".$getplayerinfo['posy']."<br>
Description: ".$getplayerinfo['desc']."<br>
</td>
</tr>
</table>
",$message);
$message=str_replace("[/playerinfo]","",$message);
}
//END OF PART

echo "$message<br>";
echo implode(",",$matches[1]);

I tried many way of listing each one , without success. The above implode returned the two names that were in the middle of playerinfo.

The above code returns 2 times the table with the same username.
 
What in the world are you trying to do here:

$message=str_replace("[playerinfo]","
<table width=90% cellspacing=1 cellpadding=3 border=0 align=center>
<tr>
<td><span class=genmed><b> $username:</b></span></td>
</tr>
<tr>
<td class=quote>
Alliance: ".$getplayerinfo['alliance']."<br>
Pointage: ".$getplayerinfo['points']."<br>
Position: X: ".$getplayerinfo['posx'].", Y: ".$getplayerinfo['posy']."<br>
Description: ".$getplayerinfo['desc']."<br>
</td>
</tr>
</table>
",$message);



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Well , I want to do an addition to a phpbb forum.

I want that when user add [playerinfo]name[/playerinfo] in his post , I want to replace [playerinfo]name[/playerinfo] by a table displaying the information about name
 
$message = "bla bla bla [playerinfo]Neomaster[/playerinfo] second player [playerinfo]Seconduser[/playerinfo]";

//START OF PART
preg_match_all ('/\[playerinfo\]([^\[]*)\[\/playerinfo\]/', $message, $matches);
foreach($matches[1] as $username){
$getplayerinfo = mysql_fetch_array(mysql_query("SELECT * FROM players WHERE username='$username'"));
$message=str_replace("[playerinfo]","
<table width=90% cellspacing=1 cellpadding=3 border=0 align=center>
<tr>
<td><span class=genmed><b> $username:</b></span></td>
</tr>
<tr>
<td class=quote>
Alliance: ".$getplayerinfo['alliance']."<br>
Pointage: ".$getplayerinfo['points']."<br>
Position: X: ".$getplayerinfo['posx'].", Y: ".$getplayerinfo['posy']."<br>
Description: ".$getplayerinfo['desc']."<br>
</td>
</tr>
</table>
",$message);
$message=str_replace("[/playerinfo]","",$message);
}
//END OF PART

echo "$message<br>";

The resulting $message gives me 2 tables but they are talkingabout the same name , the first one.
 
Nevermind.

Of course both places have the same username.

The first time you run through the loop, you replace all the matches. That means the tags for both players.

The second time the loop is run, there is nothing to replace.

As I see it, you have two options:[ol][li]instead of replacing "[playerinfo]" with the stats, replace "[playerinfo]PlayerName[/playerinfo]" with the stats. That will make each replacement specific to a user.[/li][li]tell str_replace() how many replacements to make each time it is run.[/li][/ol]


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I tried

foreach($matches[1] as $username){
$getplayerinfo = mysql_fetch_array(mysql_query("SELECT * FROM players WHERE username='$username'"));
$message = str_replace("[playerinfo]$username[/playerinfo]","
<table width=90% cellspacing=1 cellpadding=3 border=0 align=center>
<tr>
<td><span class=genmed><b>Information a propos de $username:</b></span></td>
</tr>
<tr>
<td class=quote>
Alliance: ".$getplayerinfo['alliance']."<br>
Pointage: ".$getplayerinfo['points']."<br>
Position: X: ".$getplayerinfo['posx'].", Y: ".$getplayerinfo['posy']."<br>
Description: ".$getplayerinfo['desc']."<br>
</td>
</tr>
</table>
",$message);
}

But it returns

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /home/neomaster/ on line 16

(line 16 is the $message = str_replace string)
 
It's something to do with this:

"[playerinfo]$username[/playerinfo]"

I don't know, but maybe PHP is interpreting that as some kind of array reference.

Replace the above with:

'[playerinfo]' . $username . '[/playerinfo]'




Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks , this resolved me two problem ;) You are the best :P
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top