Jump to content

[SOLVED] Searching array elements in a string


play_

Recommended Posts

What's the best way for doing this?

I have this array:
[code]$keywords = array (
"and",      "del",      "from",      "not",      "while",
"as",        "elif",      "global",    "or",        "with",
"assert",    "else",      "if",        "pass",      "yield",
"break",    "except",    "import",    "print",
"class",    "exec",      "in",        "raise",
"continue",  "finally",  "is",        "return",
"def",      "for",      "lambda",    "try"
);[/code]

I would like to search for these words in a string (and hilite them).
I am thinking, i could count the array, and use a for loop and during each loop, search and replace an element with a hilited element.

Is there a better way?
I would personally use the following array:

$keywords = array (
0 => "and",      "del",      "from",      "not",      "while",
"as",        "elif",      "global",    "or",        "with",
"assert",    "else",      "if",        "pass",      "yield",
"break",    "except",    "import",    "print",
"class",    "exec",      "in",        "raise",
"continue",  "finally",  "is",        "return",
"def",      "for",      "lambda",    "try"
);

(it would then count up from 0)

Use the following code, where $searchstring is the string you want to search through:

for each($keywords As $no => $keyword)
{
str_replace($keyword, $keywordreplacement, $searchstring);
}

RC

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.