Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 01-22-2007, 06:55 AM   #1 (permalink)
markster
Code Monkey
 
markster's Avatar
 
Join Date: Jun 2006
Posts: 65
markster is on a distinguished road
Highlight certain words in an string

What i need to do is take a string and highlight only certain words. But the words that need highlighting must come from a string that the user inputs. What I have so far:

PHP Code:
<?php
$input 
"words to be highlighted"//The word that need to be highlighted
$words explode(" ",$input); //Split the words into an array
$string "i need to have all the words in this string highlighted if they are from the array above"//The string that needs to be searched for the above words
$search_words explode(" ",$string); //Split the above string word-by-word
?>
Then I have no idea how to check the arrays against each other. Please help!
markster is offline   Reply With Quote
Old 01-22-2007, 10:25 AM   #2 (permalink)
toe_cutter
Code Monkey
 
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
toe_cutter is on a distinguished road
Send a message via ICQ to toe_cutter Send a message via AIM to toe_cutter Send a message via Yahoo to toe_cutter
Little psuedo-code

PHP Code:
foreach word_matcher in string;
  foreach 
word_string in input;
    if (
word_string == word_matcher)
      
highlight word 
__________________
toe_cutter is offline   Reply With Quote
Old 01-22-2007, 12:10 PM   #3 (permalink)
markster
Code Monkey
 
markster's Avatar
 
Join Date: Jun 2006
Posts: 65
markster is on a distinguished road
I'm running this script:

PHP Code:
<?php 
$input 
"words to be highlighted";
$words explode(" ",$input);
$string "i need to have all the words in this string highlighted if they are from the array above";
$search_words explode(" ",$string);
foreach (
$word_match in $search_words) {
    foreach (
$word_string in $words) {
        if (
$word_string==$word_match) {
            
$msg .= "<b>$word_match<b> ";
        } else {
            
$msg .= "$word_match ";
        }
    }
}
echo(
$msg);
?>
But I get this error:

Parse error: parse error, unexpected T_STRING in /home/fhlinux178/s/stuff4web.co.uk/user/htdocs/tests/desc-highlight.php on line 6
markster is offline   Reply With Quote
Old 01-22-2007, 12:32 PM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
"in" is not valid in a foreach statement in php. since you have 2 arrays, you can utilize php's array_intersect() function.

i haven't tested this code, but this may work for you.
PHP Code:
 <?php 
$input 
"words to be highlighted";
$words explode(" ",$input);
$string "i need to have all the words in this string highlighted if they are from the array above";
$search_words explode(" ",$string);

// set an array of words that match in both arrays
$matched_words array_intersect($search_words$words);

// loop through array if it is an array and there is more than 0 items
if (is_array($matched_words) && count($matched_words) > 0) {

    
// append $msg var in loop
    
foreach ($matched_words as $each) {
        
$msg .= "<b>".$each."</b> ";
    }

// set $msg error
} else {
    
$msg "There were no matches found.";
}

echo(
$msg);
?>
__________________
Mike
sde is offline   Reply With Quote
Old 01-22-2007, 10:18 PM   #5 (permalink)
markster
Code Monkey
 
markster's Avatar
 
Join Date: Jun 2006
Posts: 65
markster is on a distinguished road
how can this script be addapted to display the entire string in order and highlight the matches? right not im using the below script but it echos something like this:

i i i i need need need need to to to to have have have have all all all all the the the the words words words words in in in in this this this this string string string string highlighted highlighted highlighted highlighted if if if if they they they they are are are are from from from from the the the the array array array array above above above above

PHP Code:
<?php 
$input 
"words to be highlighted";
$words explode(" ",$input);
$string "i need to have all the words in this string highlighted if they are from the array above";
$search_words explode(" ",$string);
foreach (
$search_words as $word_match) {
    foreach (
$words as $word_string) {
        if (
$word_string==$word_match) {
            
$msg .= "<b>$word_match<b> ";
        } else {
            
$msg .= "$word_match ";
        }
    }
}
echo(
$msg);
?>
markster is offline   Reply With Quote
Old 01-22-2007, 10:32 PM   #6 (permalink)
markster
Code Monkey
 
markster's Avatar
 
Join Date: Jun 2006
Posts: 65
markster is on a distinguished road
YAY! I've had success with the str_replace() method:

PHP Code:
<?php 
$input 
"words to be highlighted";
$words explode(" ",$input);
$string "i need to have all the words in this string highlighted if they are from the array above";
foreach (
$words as $term) {
    
$string str_replace($term,"<b>$term</b>",$string);
}
echo(
$string);
?>
markster is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract Words from String Salchester MS Technologies ( ASP, VB, C#, .NET ) 3 01-01-2007 05:42 PM
Help for another program Androto Standard C, C++ 54 10-15-2004 07:21 AM
From C to Java HighterDK Java 11 07-13-2004 07:15 PM


All times are GMT -8. The time now is 11:09 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting