Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Trying to preg_match an array but only display a part of it according to user input..
Old 12-15-2009, 03:38 PM Trying to preg_match an array but only display a part of it according to user input..
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
I'm very confused. I've looked everywhere, and still can't find something that does what I need.

What I have is an array. The array contains titles for content.

Code:
Content 1 Title, Content 2 Title, Content 3 Title
They are separated by commas.

What I need is a search box that you can type in a word, and it looks through the array, looking for matches. For example, if the user typed in 'Content', it would echo:
Code:
Content 1 Title
Content 2 Title
Content 3 Title
Or if they typed '1', it would simple echo just 'Content 1 Title'.

It's turning out to be tougher than it looks, because then I am going to make it so that it knows what title belongs to which content, so it'll link to it in the title.

Like this:

Content 1 Title

Any ideas?

Thanks

-PG

BTW here's my code:

PHP Code:
$sq $_GET["sq"];

$file file("ffdatabase.php");
$FoundTitles = array();
 
foreach (
$file AS $line)
{
  if (
preg_match('~<h1>(.*)</h1>~i'$line$match)) $FoundTitles[] = $match[1];
}
 
//mrgraphic did this part, thanks :)

foreach ($FoundTitles AS $value)
{
  foreach (
explode(' '$value) AS $title)
  {
    if (
$title != ''$mainstring "<p>$title</p>";
  }

//foreach ($FoundTitles AS $value) echo "<p>$value</p>";  

if ($sq == $title) {
//echo "\"$sq\" was found in \"$mainstring\"";
}
else {
//echo "\"$sq\" was not found in \"$mainstring\"";
}

$TitleString implode(","$FoundTitles);

if(
stristr($TitleString$sq) == FALSE) {
echo 
"<p>Sorry, '$sq' was not found.</p>";
}
else { 
echo 
"<p>Search results for '$sq':</p>"
$New explode("$FoundTitles"$TitleString);
echo 
$New[0];  //Echoes all the titles
echo "<hr />";
echo 
"$FoundTitles[0] <hr />";  //Echoes the first title
$NextNew preg_match("/$sq/i""$TitleString");
echo 
$NextNew;  //This either echoes '0' or '1', meaning '1' for found, and '0' for not found.  Not needed, really...
}
/// 
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 12-15-2009 at 03:41 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-15-2009, 08:37 PM Re: Trying to preg_match an array but only display a part of it according to user inp
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Nobody? :'(
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 12-15-2009, 10:00 PM Re: Trying to preg_match an array but only display a part of it according to user inp
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
You can create a new reuslts array and add the title as a new element value for each titles array that passes a regex match.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 12-15-2009, 11:10 PM Re: Trying to preg_match an array but only display a part of it according to user inp
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Quote:
Originally Posted by mgraphic View Post
You can create a new reuslts array and add the title as a new element value for each titles array that passes a regex match.
So, something in the line of
Code:
for each element in your title array {
   if the element contains the search string (regexp) {
      add the element to the result array
   }
}
display the elements in the results array
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 12-16-2009, 03:35 PM Re: Trying to preg_match an array but only display a part of it according to user inp
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
So something like this:

PHP Code:
foreach ($TitleString AS $FoundTitles) {
  if (
$TitleString preg_match($sq)) {
       
$resultArray $foundmatch;
  }
}
echo 
$resultArray
But this doesn't work... I get this:

Warning: Invalid argument supplied for foreach() in /home/pgreviews/web/reviews.php on line 213

Line 213 is the line with foreach in it... I'm not great with foreach, or with the rest of it...
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 12-16-2009 at 03:36 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 12-16-2009, 04:40 PM Re: Trying to preg_match an array but only display a part of it according to user inp
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
foreach() requires an array to go through, I'm guessing $TitleString is a string?

PHP Code:
$titles = array("Title1""Title2""Title3");
$search "find this";  // you would get this from a $_POST-, $_GET-variable or similar
$results = array();
foreach (
$titles as $title) {
   if (
/* $title matches the $search */) {
      
$results[] = $title;
   }
}
// just to show the result
var_dump($result); 
And you should probably check the manual for preg_match().
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 12-16-2009, 04:52 PM Re: Trying to preg_match an array but only display a part of it according to user inp
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Actually, $TitleString is an array, because I originally designed it as a string, but it turned into an array...

I GOT IT WORKING!!!!!!!!!!!!!!!!!!!!!!!!!

THANKYOUTHANKYOUTHANKYOUTHANKYOUTHANKYOUTHANKYOUTH ANKYOUTHANKYOUTHANKYOUTHANKYOUTHANKYOUTHANKYOUTHAN KYOUTHANKYOUTHANKYOUTHANKYOUTHANKYOUTHANKYOU

Thank you!!!!!!!

Here's the code:

PHP Code:

$titles 
= array("title 1""2""3");
$search $sq;  // you would get this from a $_POST-, $_GET-variable or similar
$results = array();
foreach (
$titles as $title) {
   if (
stristr($title$search)) {
      
$results[] = $title;
   }
}

echo 
"<b>$results[0]</b>"
I took out the var dump thing because it just said 'NULL'. It works like this, so thank you!

Now I've gotta get it to display ALL the results, not just the first one... I'm using stristr, which is a find first occurrence in string case insensitive, so I'll find one that finds all occurrences in string, case insensitive... I'll report back soon!

For example, though, would be:

ARRAY CONTENTS: Crap Content, Good Content
SEARCH: content
OUTPUT: Crap Content
OVER-THE-TOP USER: "But I want the good content!! This site sucks, the search only displays one thing, and it's crap!"

It outputs whatever's first in the array. I need it to output all of them.

There's a function called str_replace that replaces every match with something, but I simply need it to find them all...
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 12-16-2009 at 05:11 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 12-16-2009, 05:28 PM Re: Trying to preg_match an array but only display a part of it according to user inp
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
So the problem is it only outputs the first element of the array? If that's the case then its a simple fix:

PHP Code:
$titles = array("title 1""2""3");
$search $sq;  // you would get this from a $_POST-, $_GET-variable or similar
$results = array();
foreach (
$titles as $title) {
   if (
stristr($title$search)) {
      echo 
"<b>$title</b>"
   }

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 12-17-2009, 04:56 PM Re: Trying to preg_match an array but only display a part of it according to user inp
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Thank you so much! That works perfectly!

I'm not asking for a code here, but is there a way to number the titles in $FoundTitles? Because I want to make it so the script knows where the titles came from, starting at 1, 2, 3... I'm going to link them.

Thanks
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 12-18-2009, 05:07 AM Re: Trying to preg_match an array but only display a part of it according to user inp
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Depends on how the titles are actually stored. If they are stored together with an id (such as in a database) it's another simple matter. All arrays has keys and values, if not explicity set they start counting on 0. But you could change to to start with 1.

PHP Code:
$titles = array(=> "title 1""title 2""title 3"); 
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to Trying to preg_match an array but only display a part of it according to user input..
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.31206 seconds with 12 queries