Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

759 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

16 Experts available now in Live!

Get 1:1 Help Now