Reputation: 699
I have a list of words in string. Now i want to show the separated words with link for my tags page.
Also i want to add one more condition for this tags. If the word count is less than 3 letters, then that word should not be shown in tag links.
For an example, word 'for' is should not be shown in tags link, since its 3 letter word.
$word = "Preschool Alphabet Matching Activities for Kids"
Expected Output:
<a href='tags.php?t=Preschool'>Preschool</a > <a href='tags.php?t=Alphabet'>Alphabet</a > <a href='tags.php?t=Matching'>Matching</a > <a href='tags.php?t=Activities'>Activities</a > <a href='tags.php?t=Kids'>Kids</a>
No need: <a href='tags.php?t='>for</a >
since its a 3 letter word.
Upvotes: 1
Views: 48
Reputation: 33813
$word = "Preschool Alphabet Matching Activities for Kids";
function linkify($word){
if( strlen( $word ) > 3 ) echo "<a href='tags.php?t={$word}'>{$word}</a>";
}
array_walk( explode(' ',$word ), 'linkify' );
Or, as a single liner with an anonymous function:
array_walk( explode(' ', $word ), function( $w, $k, $i=3 ){ if( strlen( $w ) > $i )echo "<a href='tags.php?t={$w}'>{$w}</a> "; } );
Upvotes: 1
Reputation: 1584
Here you go.
$word = "Preschool Alphabet Matching Activities for Kids";
$explode = explode( ' ', $word );
foreach( $explode as $words )
{
if( strlen( $words ) > 3 )
{
echo "<a href='tags.php?t=$words'>$words</a >";
}
}
Explode the string of words by the spaces, then loop through them and check the length of the word is greater than 3 and print.
Upvotes: 2