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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expressions 2

Status
Not open for further replies.

iranor

Programmer
Jun 17, 2004
174
CA
I'm a total newb in regular expressions.

Let's say I add a message in my database and I add this: [userid]5[/userid] in the message.

How can I get the number between [userid] and [/userid] ?

-- How can I replace [userid]5[/userid] with some informations? Let's say, when the user read this message, I need to get the number between them, query the database for members informations and replace [userid]5[/userid] with <a href="userinfo.php?id=5>User with id 5</a>
 
$TheString="asdasdsa[userid]5[/userid]asdasdsa";

$TheString=preg_replace("/\[userid\](\d+)\[\/userid\]/iU","<a href='userinfo.php?id=$1'>User with id $1</a>")

Known is handfull, Unknown is worldfull
 
I have one database looking like

id - username
5 - neomaster

$TheString="asdasdsa[userid]5[/userid]asdasdsa";

I need to get the data off mysql, then replace
[userid]5[/userid]
with
<a href='userinfo.php?id=5'>neomaster</a>

Is it possible? (sorry, i wasn't clear enought the first post)
 
Code:
$TheString="asdasdsa[userid]5[/userid]asdasdsa";

//connect to db
//select db
$sql= "select * from tablename";  //any where clause?
$results = mysql_query($sql);

while ($row=mysql_fetch_assoc($results))
{
  $id = $row['id'];
  $username = $row['username'];
  $rplce = "<a href = \"userinfo.php?id=$id\">$username</a>";
  $newstring[$id] = str_replace($id, $rplce, $TheString);
}

print_r($newstring);
 
sorry
change the last line of the while clause to
Code:
  $newstring[$id] = str_replace("[userid]".$id."[/userid]", $rplce, $TheString);
 
$TheString="asdasdsa[userid]5[/userid]asdasdsa";

//connect to db
//select db
$sql= "select * from tablename where id=$id"; //any where clause?
$results = mysql_query($sql);


How to take the id from $TheString to put it in the where close?

This is the code:

Table: Account
Fields: id, username
Value: 5, neomaster

--------

//Connect db
//Select db
$TheString="asdasdsa[userid]5[/userid]asdasdsa";

The user request the page:

1. I need to take the ID between [userid] and [/userid]
2. $sql = "SELECT * FROM accounts WHERE id = $id"; // from step 1
3. Replace [userid]5[/userid] with <a href="userinfo.php?id=$id>$username</a> // Take username and id from the step 2 and display it
 
how does the string ($theString) get generated?
 
$TheString is in a database, a message. I want to add some [commands][/commands] but I need the basic before. It's like a forum system / comments / news
 
still not getting a complete picture here.

is this what you are trying to do:

1. extract a message from a database.
2. IF that message contains a [user]userid[/user] expression
THEN
parse that expression to replace with a human readable equivalent of the userid via a database lookup
ELSE
leave as is?


If so, then we are nearly there. you just need to add an IF clause to the result of the database call that gives you TheString. The criteria for the IF clause will be a regular expression matching the [user] syntax.

let us know if this correctly represents your issue.

 
you are better of using RegExps here:
$TheString="asdasdsa[userid]5[/userid]asdasdsa";

preg_match_all("/\[userid\](\d+)\[\/userid\]/iU",$TheString,$TheArray);

now u can loop through the $TheArray and get the Id...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top