Solved

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

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
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…
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

690 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