Find particular keyword in a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pooh14
Forum Newbie
Posts: 23
Joined: Tue Apr 04, 2006 12:20 am

Find particular keyword in a string

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Take a look at preg_match().
(#10850)
pooh14
Forum Newbie
Posts: 23
Joined: Tue Apr 04, 2006 12:20 am

Post 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
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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:
pooh14
Forum Newbie
Posts: 23
Joined: Tue Apr 04, 2006 12:20 am

Post 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?
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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:
pooh14
Forum Newbie
Posts: 23
Joined: Tue Apr 04, 2006 12:20 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Read through all the stickies in the Regex board.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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".
pooh14
Forum Newbie
Posts: 23
Joined: Tue Apr 04, 2006 12:20 am

Post by pooh14 »

thanks alot oren.

that really help me. really appreaciate your help
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Glad I could help :P
Post Reply