Forum Moderators: coopster

Message Too Old, No Replies

Creating a link inside a string of text

Whats the way to go about this?

         

twist

8:07 pm on Apr 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This can be done in many ways, but I was wondering about how people here would approach this,

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.

twist

8:47 pm on Apr 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, how about preg_replace?


$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?

coopster

11:23 am on Apr 19, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



How will you know which portion of the text string is going to be the link (in your example "widget nuggets")? Also, how will you know what the actual link itself is going to be, you know, the href that you will wrap around the substring that you want linked?

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.

twist

8:56 pm on Apr 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The widgets that need to be linked our in another database. So I needed a function to pull the text that needed to be wrapped in a href. That text is then sent to another function which searches the other database for the correct id number and then creates the href. This is what I finally worked out.

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;
}


I was just hoping there was a built-in function that could clear up some of the above clutter. I appreciate the reply, didn't think I was going to get one :)