Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6902733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:50:05+00:00 2026-05-27T07:50:05+00:00

I am trying to work out a php function to search the referring page

  • 0

I am trying to work out a php function to search the referring page for terms and then perform a function based on the existence of those terms.

Creating the basic code wasn’t an issue, but with a fairly large number of words and optional actions, the script is getting quite long using individual lines for each group of words/function. The basic code concept is below. The stripos functions are in reverse order of preference, so that if two or more terms appear, then the most important ones are last and will over ride the previous ones

(I imagine there maybe a way to exit the script after the first condition is met, but my experiments with exit failed, so I just used reverse sequencing).

group1 = array("word1","word2","word3","word4","word5");
group2 = array("word6","word7","word8");
group3 ... etc

foreach($group1 as $groupa) { if(stripos($string, $groupa) !== false) { do something A; }  }
foreach($group2 as $groupb) { if(stripos($string, $groupb) !== false) { do something B; }  }
foreach ... etc

Is there a way to use a two dimensional array or two arrays, one with words and one with action ? ie:

words = array("word1","word2","word3","word4","word5","word6","word7","word8")
actions = array("something A","something A","something A","something A","something A","something B","something B","something B")

foreach($words as $word) { if(stripos($string, $word) !== false) { do actions; }  }

…… UPDATED ……

Inspired by Phillips suggestion, we ended up with a multidimensional array and then stepped through its “rows”. Now working on fetching the array from MySQL rather than writing it out in code.

$terms = array( 
array( "word" => "word1", 
      "cat" => "dstn",
      "value" => "XXXX" 
    ),
    ..etc
    ..etc
);
foreach ($terms as $i => $row)  
{ if(stripos($refstring3, $row['word']) !== false) { $$row['cat'] = $row['value']; }  }

…… UPDATED ……

It has evolved to a simple MySQL query, followed by a while statement rather than a foreach. Works like a charm, thanks to feedback and various other posts on Stackoverflow.

Thanks to all.

This place is great for learning and understanding, posts jump straight to the meat of things and skip having to search through numerous related but inapplicable tutorials.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:50:06+00:00Added an answer on May 27, 2026 at 7:50 am

    You could store your word-actions as a key-value array in the form of

    $actionsForWord = array("myAction" => "myword", "myAction2" => "myword2");
    

    then go through these and use Eval and string concatenation to call the function: http://php.net/manual/en/function.eval.php

    However, if you tell us more about what you actually want to achieve — i.e. what are some examples of actions you want to take, based on what words? — there may be much better and cleaner ways to organize your code. And keep in mind that Eval needs to be secured by never passing it user content, so only work with your own “whitelisted” commands.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying to work out the best method to perform logging in the application
I'm a beginner at PHP, and I'm still trying to work out proper file
I'm trying to work out why this function isn't working by placing a few
I am trying to work out how to write php functions, so that I
Trying to send a PHP email the easy way but I cannot work out
I am trying work out with MERGE statment to Insert / Update Dimension Table
I am trying to work out the best database model for the current setup:
I am trying to work out how to get the value of table cell
I'm trying to work out a way of passing the web current http context
I am trying to work out a code sample to demonstrate the debugging functionality

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.