Jump to content
php.lv forumi

bbcode [url]


anonīms

Recommended Posts

googlēju, googlēju, bet visur tiek izmantots

[url=*]*[/url]

šāds variants man neder, jo nepieciešams

[url][/url]

pašlaik kods ir šāds:

function forum_text($text)
{

 $bbcode = array("<", ">",
               "[img=", "]", 
               "[b]", "[/b]", 
               "[u]", "[/u]", 
               "[i]", "[/i]",
               '[url]', "[/url]",
               '"]');
 $htmlcode = array("<", ">",
               "<br /><img src=\"", "\">", 
               "<b>", "</b>", 
               "<u>", "</u>", 
               "<i>", "</i>",
               '<a href="', "</a>",
               '">');



 $newtext = str_replace($bbcode, $htmlcode, $text);
 $newtext = nl2br($newtext);
 return $newtext;
}

 

Kā dabūt gatavu to ?

 

EDIT:

'/\[url\](.*?)\[\/url\]/is'
vs
'<a href="$1">$1</a>',

šādi ar neiet

Edited by anonīms
Link to comment
Share on other sites

Dabuju gatavu jau šādi

function forum_text($str)
{
$bb = array(
'/\[b\](.*?)\[\/b\]/is',
'/\[i\](.*?)\[\/i\]/is',
'/\[u\](.*?)\[\/u\]/is',
'/\[img\](.*?)\[\/img\]/is',
'/\[url\](.*?)\[\/url\]/is'
);

$replace = array(
"<strong>$1</strong>",
"<i>$1</i>",
"<u>$1</u>",
"<br /><img src='$1' alt='' /><br />",
"<a href='http://$1' title='$1' target='_blank' rel='nofollow'>$1</a>"
);

$str = preg_replace($bb,$replace,$str);

return $str;
}

Link to comment
Share on other sites

Tātad, ja pareizi sapratu, tad normāls variants (+ bez ievainojumiem) būtu šāds:

function forum_text($str)
{
$str = htmlspecialchars($str);

$str = preg_replace( "#\[url\](?:http:\/\/)?(.+?)\[/url\]#is", "<a href=\"http://$1\">$1</a>", $str ); 
$str = preg_replace( "#\[img\](?:http:\/\/)?(.+?)\[/img\]#is", "<img src=\"http://$1\" />", $str ); 
$str = preg_replace( "#\[b\](.+?)\[/b\]#is", "<strong>$1</strong>", $str ); 
$str = preg_replace( "#\[i\](.+?)\[/i\]#is", "<i>$1</i>", $str ); 
$str = preg_replace( "#\[u\](.+?)\[/u\]#is", "<u>$1</u>", $str ); 

return $str;
}

 

Labojiet, ja maldos

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...