very simple parsing of tags

Discussion in 'PHP' started by gordi555, Jun 15, 2007.

  1. #1
    Hi all,

    Sorry but need your help.

    Basically I want to store records like...

    "added <profile>gordi555</profile> as a friend" in a mysql record and need a bit of code to turn that into...

    "added <a href="profile.php?user=gordi555">gordi555</a> as a friend"

    I need to also add other tags in the function like <url> and <albumid>13432</album>

    Do you get what I mean?

    I've tried google but can't think of the best things to look for to find the answer - could anyone help please?

    Simplest way would be great thanks anyway :) thanks in advance
     
    gordi555, Jun 15, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    function parse_tags($text)
    {
    	return preg_replace(array(
    			'/<profile>([^<]+)<\/profile>/i',
    			'/<url>([^<]+)<\/url>/i',
    			'/<album>([^<]+)<\/album>/i'
    		), array(
    			'<a href="profile.php?user=$1">$1</a>',
    			'<a href="$1">$1</a>',
    			'<a href="album.php?id=$1">$1</a>'
    		),	
    		$text
    	);
    }
    
    
    PHP:
     
    nico_swd, Jun 15, 2007 IP
  3. gordi555

    gordi555 Active Member

    Messages:
    537
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #3
    nico_swd, thanks a million, simple and effective - very many thanks! Respect coming your way!
     
    gordi555, Jun 15, 2007 IP
  4. dankenpo

    dankenpo Guest

    Messages:
    30
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If I understand what you are saying:

    First we need a database, so let's get into phpmyadmin (or whatever you use) and make one with all of the fields we will need (Add/Delete for your needs)
    
    CREATE TABLE profiles (userid VARCHAR(30), password VARCHAR(30), 
    albumid VARCHAR(30), about VARCHAR(30)); 
    INSERT INTO users VALUES ( "gordi555", "mypassword", "g1", 
    "Crazy man Gordi" ), ( "dankenpo", "md5pw", "d1", "Dan the man" )
    
    Code (markup):
    This will create a table for us to work with, that holds a userid, password, albumid, and a short description of each user (which could be their registration name).

    First we need to connect to the database:
    
    <?php
    mysql_connect("your.hostaddress.com", "username", "password") or
    die(mysql_error()); 
    mysql_select_db("Database_Name") or die(mysql_error()); 
    ?> 
    
    Code (markup):
    Next we will retrieve the information from the database table and we will temporarily put this information into an array ($userinfo) to use:
    
    $data = mysql_query("SELECT * FROM profiles") 
    or die(mysql_error());
    $userinfo = mysql_fetch_array( $data );
    
    Code (markup):
    Now test it out to make sure it works:
    
    echo "Your username is: ".$userinfo['userid'] . " "; 
    echo "<a href=\"profile.php?user=\"".$userinfo['userid']."\">Your Profile</a>"; 
    
    Code (markup):
    Something like that or am I way off?
     
    dankenpo, Jun 15, 2007 IP
  5. gordi555

    gordi555 Active Member

    Messages:
    537
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Thanks for your help dankenpo but nico_swd was spot on for my needs!

    Just wondering how do you add respect to someone? I'm thick today :)
     
    gordi555, Jun 15, 2007 IP
  6. gordi555

    gordi555 Active Member

    Messages:
    537
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #6
    While we here :)

    How about a tag like <album name="my album">123882</album>

    Thanks for the help - what would be the search term for google so I dont have to bother you nice people?

    Cheers!
     
    gordi555, Jun 15, 2007 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    What do you want to replace this with?


    As for Google, it'd be something like "Regular expression tutorial". This site may as well help.

    www.php.net/preg_replace
     
    nico_swd, Jun 15, 2007 IP
    gordi555 likes this.
  8. gordi555

    gordi555 Active Member

    Messages:
    537
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #8
    <album name="my album">123882</album> -> <a href="album.php?id=123882">my album</a>

    Many thanks again :)
     
    gordi555, Jun 15, 2007 IP
  9. dankenpo

    dankenpo Guest

    Messages:
    30
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Also, gordi55, you add to people's reputations by clicking what looks like a "scale" icon in the top right hand corner of any thread. G'day!
     
    dankenpo, Jun 15, 2007 IP
    gordi555 likes this.
  10. gordi555

    gordi555 Active Member

    Messages:
    537
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #10
    Thanks dan :)
     
    gordi555, Jun 15, 2007 IP
  11. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #11
    In the code I gave you first, add this at the bottom of the first array:
    
    '/<album\s+name=(["\'])([^\\1]+)\\1>(\d+)<\/album>/i'
    
    PHP:
    And this at the bottom of the second:
    
    '<a href="album.php?id=$3">$2</a>'
    
    PHP:
     
    nico_swd, Jun 15, 2007 IP
  12. gordi555

    gordi555 Active Member

    Messages:
    537
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #12
    Again, nico_swd, simply spot on :) thanks a trillion!!!
     
    gordi555, Jun 15, 2007 IP