GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Jun-2003, 15:30
misunderstood misunderstood is offline
Member
 
Join Date: Jun 2003
Posts: 121
misunderstood is on a distinguished road

str_replace and links


This isnt a question for a change but just a suggestion for beginners like me

If you want a user to be able to personalise their input area the following are very useful. I wont explain them but the last one is ideal for activating links typed in by the user.

PHP Code:

$description = str_replace("[b ]","<b>",$description); 
$description = str_replace("[/b ]","</b>",$description);
$description = str_replace("[u ]","<u>",$description); 
$description = str_replace("[/u ]","</u>",$description);
$description = str_replace("[i ]","<i>",$description); 
$description = str_replace("[/i ]","</i>",$description);
$description = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
                     "<a href=\"\\0\">\\0</a>", $description); 



I have just thought of something, If anybody can make a suggestion on how to make the link open in a new page that would be great.
Ignore the first line of this posting about there not being a question.
  #2  
Old 30-Jun-2003, 03:04
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Processing BBCodes - using str_replace() / strtr()


There's nothing wrong with your code so far, although my personal preference is NOT to use ereg_replace() anywhere in my codes - a much faster alternative is preg_replace().

Here are a few 'alternatives' to your existing codes above:

PHP Code:

<?php

// put all the formatting replacements in an array

$bbcodes = array(
  '[b ]' => '<strong>',
  '[/b ]' => '</strong>',
  '[u ]' => '<u>',
  '[/u ]' => '</u>',
  '[i ]' => '<em>',
  '[/i ]' => '</em>'
);

// I. str_replace() method
// ------------------------
  $find = array_keys( $bbcodes );
  // process any string with formatting bbcodes
  $str = str_replace( $find, $bbcodes, $str );

// II. strtr() method
// ---------------------
  // process any string with formatting bbcodes
  $str = strtr( $str, $bbcodes );
?>


The cool thing about using an array is that you can add to this array during the time the script is run before finally converting all the bbcodes off a string / text.

re: the link opening in a new window (using the code you supplied above):

PHP Code:

<?php
$description = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
                     "<a href=\"\0\" target=\"_blank\">\0</a>", $description);
?>

  #3  
Old 30-Jun-2003, 04:00
misunderstood misunderstood is offline
Member
 
Join Date: Jun 2003
Posts: 121
misunderstood is on a distinguished road
For some reason between now and yesterdays posting the activate link code doesnt seem to work
It seems to affect or be affected by other links below it.

OK back to the drawing board

I think I will start exploring preg_replace but any suggestions or articles welcome.

ps JDS is using array a faster method or are there other benefits?
  #4  
Old 30-Jun-2003, 05:57
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
P.S. Before the code you've posted up "misunderstood" you should have called htmlspecialchars() on $description, to make any other HTML code in the post safe.

GF
  #5  
Old 30-Jun-2003, 08:10
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Having your 'default' bbcodes in an array (or working with them through an array) means that you can store all these 'default', formatting-type, bbcodes in a flatfile or database. It just adds to the overall efficiency when you want to add, modify or delete... whenever.

Imagine adding a bbcode like [xxx] or [yyy] in the future! Having these replacements hard-coded in your scripts means you'd have to dig through the script or function at a later date and you certainly want to avoid that in the way that you code PHP scripts generally.

Also, like I said in my previous post, you can add to this array on-the-fly, a good idea when you want to process or type bbcodes. Here's an example post where Allowee and I discuss adding the codes generally. For clarity, in that example, I used 2 arrays, one named '$replacements' and one named '$smilies'; they could have just as well been merged into one array named '$bbcodes'!...

Incidentally, this is EXACTLY how I added the custom [html ], [css ] and [c++ ] bbcodes for these forums here.

Finally, if it is not already obvious, working off an array like this means you only process ONE str_replace() statement to process ALL your bbcodes!
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:29.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.