Forum Moderators: coopster

Message Too Old, No Replies

Problem with a php script

         

turbohost

6:13 pm on Dec 15, 2004 (gmt 0)

10+ Year Member



Hi Guys,

Got a problem with a php script.

I've got a variable $page holding a text. After that I do a str_replace to replace some recurring words with a single WORD (this will make my query easier). After that I do a preg_match looking for the specific WORD and some words after this WORD. The WORD occurs multiple times in the text though and I only get the first WORD. How can I get all the other WRDS?

Here is a piece of the script :

$page = $contents;
$pageis .= htmlspecialchars($page) . "<br>\n";
$pagefiltered = str_replace("bla bla bla","WORD",$pageis);
preg_match('/WORD(.+?)\/(.+?)\//im',$pagefiltered,$result);
echo "$result[1],$result[2]";

Greetz,
Turbo

mincklerstraat

6:20 pm on Dec 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Greetings to you, Turbo.

You might find your answer in

preg_match_all() [be2.php.net]
.

Happy coding!

StupidScript

6:29 pm on Dec 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For example:

$page = $contents;

$pageis .= htmlspecialchars($page) . "<br>\n";

$pagefiltered = str_replace("bla bla bla","WORD",$pageis);

preg_match_all('/WORD/',$pagefiltered,$result);

echo sizeof($result[0])." <br />\n";

for($i=0;$i<sizeof($result[0]);$i++) {

echo $result[0][$i];

if ($i!=sizeof($result[0])-1) { echo ","; }

}

(I simplified the regexp because I didn't get any matches with it during testing.)

turbohost

7:50 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



Thx people, it works great :->