?
Solved

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

Posted on 2007-11-14
2
Medium Priority
?
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 20

Accepted Solution

by:
steelseth12 earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses how to implement server side field validation and display customized error messages to the client.
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.
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 …
Suggested Courses

743 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