Solved

php need to find random word over 5 characters and replace it

Posted on 2007-11-14
2
215 Views
Last Modified: 2013-12-13
I am building a list and want to take one word in each list item and highlight it. I do not want to highlight a word like "a", "and", or "the" so I want find words over 4 or 5 characters. When found I need to put a SPAN or FONT tag around it and replace it in the string.

      while(list($highlight) = db_fetch_array($qid_hl)) {
            $words = explode("\s", $highlight);
            $keyword = $words[rand(0, count($words)-1)];
                $newword = "<font color=\"red\">".$keyword."</font>";
                $highlight = str_replace($keyword,$newword,$highlight);
      }

This does not take into account the size of the word and it sure seems like a lot of lines of code to do what I need to do. ANy siggestions and help are greatly apprecaited.

Thom
0
Comment
Question by:td234
2 Comments
 
LVL 20

Accepted Solution

by:
steelseth12 earned 500 total points
ID: 20282267
while(list($highlight) = db_fetch_array($qid_hl)) {
           
                  preg_match_all("/\S{5,}/i",$highlight,$matches);

                  shuffle($matches[0]);
                            
                  $newword = "<font color=\"red\">".$matches[0][0]."</font>";
           
                  $highlight = str_replace($matches[0][0],$newword,$highlight);
     
}
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20282305
This will check the word and make sure it is over 5 characters before highlighting:


<?PHP
while(list($highlight) = db_fetch_array($qid_hl))
{
            $words = explode("\s", $highlight);
           
            $keyword;
            $count=0;
            while(strlen($keyword) < 5 && $count < count($words))
            {
                  $keyword = $words[rand(0, count($words)-1)];
                  $count++;
            }
            if($count == count($words))
            {
                  die("Error! No word is longer than 5 characters!");
            }
            $newword = "<font color=\"red\">".$keyword."</font>";
            $highlight = str_replace($keyword,$newword,$highlight);
      }

?>
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question