Solved

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

Posted on 2007-11-14
2
211 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now