Find particular keyword in a string
Moderator: General Moderators
Find particular keyword in a string
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.
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Helo thanks for the reply.
Assuming i have a
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
Assuming i have a
Code: Select all
$mystring = "this is a include('test.php'). I would like to test include('pooh.php'); "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
Run this script:
The result should look like this:
Now you can do the rest I guess... if not, please post here and we'll help you 
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);
?>Code: Select all
Array
(
[0] => Array
(
[0] => include('test.php')
[1] => include('pooh.php')
)
[1] => Array
(
[0] => test.php
[1] => pooh.php
)
)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?
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?
Ok, use this code:
Note that now the returned array looks like this:
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
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);
?>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
)
)If you have any other questions feel free to ask
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
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
thank you so much again
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"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
thank you so much again
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".
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".