Forum Moderators: coopster
First I want to grab a string of text from a database like so,
$query = "SELECT text FROM $database WHERE id = '$id'"; The text will look something like this,
echo "This is some text about widget nuggets and how they work"; What I want to do is put the word "widget nuggets" inside a function that will turn it into a link like this,
echo "This is some text about ". link( 'widget nuggets' ) ." and how they work"; My first thought on how to approach this would be to add some type of identifier around the text in the database I want to be turned into a link like so,
"This is some text about ^widget nuggets* and how they work"
What symbols would you use? What symbols are allowed in mysql?
Anyway, now that i've marked the text to be put inside a function what would be the best approach to searching the string for this text?
My current thought would be to count the length of the string and then do a substr search for the occurences of the characters and insert the function call in the appropriate places. I am also thinking about preg_match but I am thinking there is a better and more effecient way to do this.
$string = "This is some text about ^widget nuggets* and how they work";$symbol = "/^/";
$replacement = "\". link( \'";
$string = preg_replace($symbol, $replacement, $string);
$symbol = "/*/";
$replacement = ". \"";
$string = preg_replace($symbol, $replacement, $string);
echo $string
Will this work? Is there a better way to do this?
If the text string is truly going to be embedded in HTML, why not just store the HTML in the string column in the database as such? If you don't need the HTML, use PHP's strip_tags() [php.net] function.
This is what the text in database looks like,
"This is a string of text about %5widget stuff~ in a database"
I check for a '%' then a number '5' to determine which kind of link it is, then I check for the '~' to show the end of the links
function CreateLink( $text, $size, $cat ) {
--$output = '';
--$start = false;
--$text_cat = false;--for( $loop = 0; $loop < strlen( $text ); $loop++ ) {
------if( $text[ $loop ] == '~' ) {
--------$start = false;
--------if( $cat == $category ) { $output .= $link; }
--------else { $output .= LinkTo( $category, $size, $link ); }
------}
------else if( $text_cat == true ) { $text_cat = false; $category = $text[ $loop ]; }
------else if( $start == true ) { $link .= $text[ $loop ]; }
------else if( $text[ $loop ] == '%' ) { $start = true; $text_cat = true; $link = ''; }
------else { $output .= $text[ $loop ]; }
--}
--return $output;
}