Forum Moderators: coopster

Message Too Old, No Replies

Removing Short Words Under 4 Letters

         

phpseeker

10:39 am on Jun 27, 2011 (gmt 0)

10+ Year Member



Hi,

I have a title tag module but i want to edit this like that, i want to remove short words, under 4 letters, here is the sentences but it must be like that:

current: Jack and Jenny are going to the bus stop
must be: Jack Jenny going stop

How can i do that? here is the code...

 <? 
$tagArray = array();

$tags = "Jack and Jenny are going to the bus stop";

foreach (str_word_count($tags,1,'áé&#8216;') as $tag )

{
array_key_exists ( $tag, $tagArray ) ? $tagArray[ $tag ]++ : $tagArray[ $tag ] = 0;
}

function clouds( $tagArray = array() )
{
foreach( $tagArray as $tag => $say )

{
$tag=mb_strtolower($tag, 'UTF-8');

$cloudArray[] = '<a class="tag" href="tags/'.$tag.'" title="'.$tag.'">'. htmlspecialchars( stripslashes( $tag ) ) . '</a>';


}

return join( "\n", $cloudArray ) . "\n";
}
echo clouds( $tagArray );

?>


also below code is doing it but how can i integrate this code to above code?

 <?php

$str = 'Jack and Jenny are going to the bus stop';
$words = explode(' ', trim($str));
$strOut = '';
foreach ($words as $word) {
if (strlen($word) > 3) {$strOut .= $word . ' ';}
}
$strOut = trim($strOut);
echo $strOut; //output string

?>

brotherhood of LAN

11:04 am on Jun 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to the forums phpseeker,

Hopefully this is helpful

function string_to_cloud($string) { $cloudArray = array(); $tags = preg_split("'\s+'",trim(preg_replace("'\b[a-z0-9]{1,3}\b'i",' ',$string))); foreach($tags as $tag) $cloudArray[] = ''. htmlspecialchars(stripslashes($tag)).''; return implode("\n",$cloudArray); } echo $cloudArray = string_to_cloud('Jack and Jenny are going to the bus stop ');

  • Function accepts a string of words and converts them to a list of links with each word in the string that is >3 chars in length
  • Word is defined as alphanumeric characters, A-Z and 0-9... case insensitive as denoted by the "i" flag
  • preg_replace uses word boundaries [regular-expressions.info] to match words of 1-3 chars, and replaces them with a space
  • preg_split splits the string into words, regardless of the number of spaces between words
  • array is walked through to generate <a> tags
  • array imploded and returned as string

    // Added
    If you want to ensure unique tags, I'd just change $cloudArray[] to $cloudArray[$tag]
  • phpseeker

    11:28 am on Jun 27, 2011 (gmt 0)

    10+ Year Member



    very helpful thank you but also i want to use some function for this code, for example:

    deletes duplicate words
    [php]array_key_exists ( $tag, $tagArray ) ? $tagArray[ $tag ]++ : $tagArray[ $tag ] = 0; [/php]

    showing special characters

    foreach (str_word_count($tags,1,'áéíóú&#8216;') as $tag )


    making letters lowercase
    $tag=mb_strtolower($tag, 'UTF-8');


    can i use these functions on your code? and how? thanks...

    penders

    11:37 am on Jun 27, 2011 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    brotherhood of LAN's function looks far more whizz, however, you could just add 1 line of code to your current script to filter out your short words...

    function clouds( $tagArray = array() ) 
    {
    foreach( $tagArray as $tag => $say )
    {
    if (strlen($tag) < 4) continue;
    :


    @brotherhood of LAN: Will preg_replace() produce the same output as str_word_count() in this case? str_word_count() is locale aware and phpseeker has included an additional charlist.

    @phpseeker: Do you intend to use the count of each word to render your 'cloud'. This seems to be an unnecessary extra step currently.

    phpseeker

    11:42 am on Jun 27, 2011 (gmt 0)

    10+ Year Member



    @penders, can you paste full code please, i added but not worked

    penders

    11:58 am on Jun 27, 2011 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    function clouds( $tagArray = array() ) 
    {
    foreach( $tagArray as $tag => $say )
    {
    if (strlen($tag) < 4) continue;
    $tag=mb_strtolower($tag, 'UTF-8');
    $cloudArray[] = '<a class="tag" href="tags/'.$tag.'" title="'.$tag.'">'. htmlspecialchars( stripslashes( $tag ) ) . '</a>';
    }
    return join( "\n", $cloudArray ) . "\n";
    }


    The line in red is the only line added to your current script. Admittedly I've not actually tested this, but I can't see why it wouldn't work. Do you get an error, or is it just not filtering out the small words?

    [edited by: penders at 12:00 pm (utc) on Jun 27, 2011]

    brotherhood of LAN

    12:00 pm on Jun 27, 2011 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    phpseeker, it looks like if you add the line penders has highlighted in red, your script should do exactly as intended using your initial PHP + their one line solution.

    penders... you're right, i skimmed the OP and missed that. preg_split can indeed get the same effect I believe, using the 'u' modifier. You can also reference characters using octal codes, however I've been lazy and copied/pasted the list of special chars into the regex here:

    function string_to_cloud($string) { $cloudArray = array(); $tags = preg_split("'\s+'",trim(preg_replace("'[^áéíóúa-z0-9‘][áéíóúa-z0-9‘]{1,3}[^áéíóúa-z0-9‘]'iu",' ',mb_strtolower(htmlspecialchars(stripslashes($string)),'UTF-8')))); foreach($tags as $tag) $cloudArray[$tag] = ''.$tag.''; return implode("\n",$cloudArray); } echo $cloudArray = string_to_cloud('Jackáéíóú and Jeáéíóúnny are going to the bus stop ');

    Try running it again, I had an extra bracket in there causing the error

    [edited by: brotherhood_of_LAN at 12:13 pm (utc) on Jun 27, 2011]

    phpseeker

    12:09 pm on Jun 27, 2011 (gmt 0)

    10+ Year Member



    it gave error, i think there is problem here ("'[^áéíóúa-z0-9‘

    phpseeker

    1:03 pm on Jun 27, 2011 (gmt 0)

    10+ Year Member



    @brotherhood of LAN, thank you very much but @penders edited code worked for me.

    @penders, also thanks a lot your code is worked perfectly with my current code, this is what i want, thanks again...