Re: Breaking up a line and using the text?

From: Date: Mon, 05 Feb 2001 15:05:43 +0000
Subject: Re: Breaking up a line and using the text?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-38242@lists.php.net to get a copy of this message
"Sam" <Sam@dragoman.co.uk> wrote: > I was wondering if it is possible to break up a line of text from a > database, and split it into separate links, > Original line > Hi my name is Sam Rose > > Which can then be broken down into > Hi > My > Names > is > Sam Rose Sam, do you want to turn them into hyperlinks? If your data is really that clean explode() should do the trick, otherwise you may want to look at split(). Here's a solution using explode(). See the PHP manual for details. FYI, unless you have some way planned for grouping together words (like "Sam Rose") the PHP code will split them apart. // $string is a string pulled from a database. // explode() below splits $string into an array by splitting into array elements when encounters " " in code. // In your example, $words[0] = "Hi"; $words[1] = "My"; ...; $words[5] = "Rose"; $words = explode( " ", $string ); // Loop through array and build string $output containing hyperlinks. for ( $i = 0; $i < count( $words ); $i++ ) { $output .= "<a href='/{$words[$i]}.html'>{$words[$i]}</a>"; } // Print hyperlinks. echo $output; -- Steve Werby COO 24-7 Computer Services, LLC Tel: 804.817.2470 http://www.247computing.com/

« previous php.general (#38242) next »