Page 1 of 1

Find particular keyword in a string

Posted: Fri Apr 14, 2006 12:54 am
by pooh14
Hi Guys,

I would to know how to find for a particular keyword in a string.

Example, i would like to have all the the words include('XXXX') in the string in an array.
It doesnt matter what is inside the bracket of the include statement

Thanks in advanced.

Posted: Fri Apr 14, 2006 12:58 am
by Christopher
Take a look at preg_match().

Posted: Fri Apr 14, 2006 4:53 am
by pooh14
Helo thanks for the reply.


Assuming i have a

Code: Select all

$mystring = "this is a include('test.php'). I would like to test include('pooh.php'); "
so i want all the filename inside the include in the string in an array is possible.

meaning
array[0] = test.php
array[1] = pooh.php

i am not sure on how to get the preg_match thing working for this
thanks in advanced

Posted: Fri Apr 14, 2006 5:14 am
by Oren
Run this script:

Code: Select all

<?php

	$x = "this is a include('test.php'). I would like to test include('pooh.php');";

	preg_match_all("/include\(\'(.+\.php)\'\)/iU", $x, $arr);

	print_r($arr);

?>
The result should look like this:

Code: Select all

Array
(
    [0] => Array
        (
            [0] => include('test.php')
            [1] => include('pooh.php')
        )

    [1] => Array
        (
            [0] => test.php
            [1] => pooh.php
        )

)
Now you can do the rest I guess... if not, please post here and we'll help you :wink:

Posted: Fri Apr 14, 2006 6:48 am
by pooh14
Thank Oren.

I have 2 questions though

One is is also want .html files inside the include statement too. at the moment only if the file is .php inside include statement it is detecting.

Second question is how do this preg_match_all pattern thing work? I just dont get the hang of how to put the conditions?

Posted: Fri Apr 14, 2006 7:22 am
by Oren
Ok, use this code:

Code: Select all

<?php

	$x = "this is a include('test.php'). I would like to test include('pooh.html');";

	preg_match_all("/include\(\'(.+\.(php|html))\'\)/iU", $x, $arr);

	print_r($arr);

?>
Note that now the returned array looks like this:

Code: Select all

Array
(
    [0] => Array
        (
            [0] => include('test.php')
            [1] => include('pooh.html')
        )

    [1] => Array
        (
            [0] => test.php
            [1] => pooh.html
        )

    [2] => Array
        (
            [0] => php
            [1] => html
        )
)
As for your second question: http://us2.php.net/manual/en/function.p ... ch-all.php

If you have any other questions feel free to ask :wink:

Posted: Fri Apr 14, 2006 8:22 am
by pooh14
Thanks again Oren.

Now for explaination time. I Did check out the preg_match manual. but what i dont understand is how did u come out with the pattern string

Code: Select all

"/include\(\'(.+\.(php|html))\'\)/iU"
i know we start of with / and end with / but what is iU doing at the back?

what about this + sign and \. ( when do you use them?

sorry for asking so many silly question but I am totally new to this and php :oops:

thank you so much again

Posted: Fri Apr 14, 2006 8:28 am
by feyd
Read through all the stickies in the Regex board.

Posted: Fri Apr 14, 2006 8:47 am
by Oren
Ok it goes like this:

After the / and / come the Pattern Modifiers
The modifiers declare the behaviour of the match pattern.
'i' - Means that letters in the match pattern are case insensitive.
'U' - This modifier inverts the "greediness" of the quantifiers so that they are not greedy. Without the 'U' it would caught this: include('test.php'). I would like to test include('pooh.html')
(see the Pattern Modifiers for more info).
'.' - In regular expressions the '.' means any character (except new lines) and therefore when you want to refer the '.' to a simple dot then you must escape it using a backslashe like this: \.
'+' - Means "at least one time".

Posted: Fri Apr 14, 2006 9:32 am
by pooh14
thanks alot oren.

that really help me. really appreaciate your help

Posted: Fri Apr 14, 2006 9:47 am
by Oren
Glad I could help :P