Short but tough. Need help with these two lines...

Discussion in 'PHP' started by x0x, Jan 12, 2010.

  1. #1
    Basically what I am trying to do is to create custom tags that can be used on my site to create links to other player profiles (it's a game site).

    I got this far:

    if(strpos($var,'[pl]') && strpos($var,'[epl]')){;
      $var = str_replace("[pl]$var[epl]", "<a href=\"player.php?\mob=$var\">$var</a>", $var);
    	}
    PHP:
    Basically it looks if the $var (input) contains the starting tag [pl] and ending tag [epl] (it didn't let me use [/pl] in str_replace, so I'm using [epl] instead.

    But I just realized that it's totally wrong... The key question here is how to assign the text between the tags to a new variable (from the input, so I can make the link work properly)? Right now it treats the whole text ($var) as a link and isn't functioning as it should..
     
    x0x, Jan 12, 2010 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You should consider using a regular expression for this. use
    $var = preg_replace('/[pl](.*?)[\/pl]/', '<a href="player.php?mob=$1">$1</a>', $var);
    PHP:
    That will match between the [pl] and [/pl] for you, capture whatever it is and put it in the a's href and content
     
    JAY6390, Jan 12, 2010 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    You most likely need to need to use preg_replace. Can you give an example of what the variable $var is before you start the strpos functions? I'm not clear on what you;re trying to accomplish by the str_replace.
     
    jestep, Jan 12, 2010 IP
  4. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #4
    I don't think it's working properly... Here is what happened:

    $var = "This is a test message [pl]Bull[/pl] - that is a player name that should turn into a hyperlink.";
    PHP:
    Here is what is usually done with the $var

    $var = str_replace("\n","<br>", $var);
        $var = str_replace("[b]","<b>", $var); $var = str_replace("[/b]","</b>", $var);
        $var = str_replace("[i]","<i>", $var);$var = str_replace("[/i]","</i>", $var);
        $var = str_replace("[u]","<u>", $var);$var = str_replace("[/u]","</u>", $var);
    	
    	
    PHP:
    Now, I've added your line to the end:

    $var = preg_replace('/[pl](.*?)[\/pl]/', '<a href="player.php?mob=$1">$1</a>', $var);
    PHP:
    The result:
    
    This is a test message [<a href="player.php?mob="></a>]Bu<a href="player.php?mob="></a>[/<a href="player.php?mob="></a>] - that is a <a href="player.php?mob="></a>ayer name that shou<a href="player.php?mob=d%20turn%20into%20a%20hy">d turn into a hy</a>erlink.
    PHP:

    I think I answered your question up there. $var is basically raw user input that is formatted for the database.



    There was some kind of a trick to extract the text between the tags, but I can't remember it. Regex is a nice way to do it too though, but I suck at that to be honest.
     
    x0x, Jan 12, 2010 IP
  5. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Oops my original pattern should have been
    /\[pl\](.*?)\[\/pl\]/
    Try that
     
    JAY6390, Jan 12, 2010 IP
  6. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #6
    That worked. Thank you!
     
    x0x, Jan 12, 2010 IP