Jump to content


Photo

Find particular word in a string


  • Please log in to reply
6 replies to this topic

#1 laavanya14

laavanya14
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 14 April 2006 - 05:56 AM

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

I checked out the preg_match manual, but not really sure how to use it.

Thanks in advanced.

#2 bonaparte

bonaparte
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts
  • Location#172, I Floor, 'Hamsashree', Defence Colony, Sahakaranagar, Bangalore, India

Posted 14 April 2006 - 06:43 AM

[!--quoteo(post=364707:date=Apr 14 2006, 12:56 AM:name=Laavanya)--][div class=\'quotetop\']QUOTE(Laavanya @ Apr 14 2006, 12:56 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
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

I checked out the preg_match manual, but not really sure how to use it.

Thanks in advanced.
[/quote]

Sorry, it is a bit difficult to understand the query. preg_match() matches an regular expression in a string and can store the results in an array.

Could you expain your example with more information? If you can tell us what exactly do you want to do, we might help you with the code.

#3 laavanya14

laavanya14
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 14 April 2006 - 09:52 AM

Helo thanks for the reply.


Assuming i have a
$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

thanks in advanced


#4 bonaparte

bonaparte
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts
  • Location#172, I Floor, 'Hamsashree', Defence Colony, Sahakaranagar, Bangalore, India

Posted 14 April 2006 - 10:46 AM

[!--quoteo(post=364714:date=Apr 14 2006, 01:43 AM:name=bonaparte)--][div class=\'quotetop\']QUOTE(bonaparte @ Apr 14 2006, 01:43 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Sorry, it is a bit difficult to understand the query. preg_match() matches an regular expression in a string and can store the results in an array.

Could you expain your example with more information? If you can tell us what exactly do you want to do, we might help you with the code.
[/quote]


See if this works for you:

$mystring = "this is a include('test.php'). I would like to test include('pooh.php'); ";
$pattern = "/([a-z])*\.php/";
$subject = $mystring;

preg_match_all($pattern, $subject, $matches);
print_r($matches);


[!--quoteo(post=364746:date=Apr 14 2006, 05:40 AM:name=bonaparte)--][div class=\'quotetop\']QUOTE(bonaparte @ Apr 14 2006, 05:40 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
See if this works for you:

$mystring = "this is a include('test.php'). I would like to test include('pooh.php'); ";
$pattern = "/([a-z])*\.php/";
$subject = $mystring;

preg_match_all($pattern, $subject, $matches);
print_r($matches);
[/quote]


Sorry, the below code seems to be more effective:


$mystring = "this is a include('test.php'). I would like to test include('pooh.php'); ";
$pattern = "/[a-zA-Z]+\.php/";
$subject = $mystring;

preg_match_all($pattern, $subject, $matches);
print_r($matches);
foreach ($matches as $value) {
$targetarray = $value;
}
echo "<br> target array ";
print_r($targetarray);


#5 laavanya14

laavanya14
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 14 April 2006 - 10:53 AM

[!--quoteo(post=364746:date=Apr 14 2006, 05:46 AM:name=bonaparte)--][div class=\'quotetop\']QUOTE(bonaparte @ Apr 14 2006, 05:46 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
See if this works for you:

$mystring = "this is a include('test.php'). I would like to test include('pooh.php'); ";
$pattern = "/([a-z])*\.php/";
$subject = $mystring;

preg_match_all($pattern, $subject, $matches);
print_r($matches);
Sorry, the below code seems to be more effective:
$mystring = "this is a include('test.php'). I would like to test include('pooh.php'); ";
$pattern = "/[a-zA-Z]+\.php/";
$subject = $mystring;

preg_match_all($pattern, $subject, $matches);
print_r($matches);
foreach ($matches as $value) {
$targetarray = $value;
}
echo "<br> target array ";
print_r($targetarray);
[/quote]


helo, thank you so very much.
however there is a constraint here.
i only want the statement printed if it is inside the include statement. besides it not neccesary be a php file. it might be a html file.

the particular code would even print out even if the filename isnt inside the include function.
basically what i am trying to do here, is to parse the string and check for the included files.

thanks again

#6 bonaparte

bonaparte
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts
  • Location#172, I Floor, 'Hamsashree', Defence Colony, Sahakaranagar, Bangalore, India

Posted 14 April 2006 - 10:59 AM

[!--quoteo(post=364749:date=Apr 14 2006, 05:53 AM:name=Laavanya)--][div class=\'quotetop\']QUOTE(Laavanya @ Apr 14 2006, 05:53 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
helo, thank you so very much.
however there is a constraint here.
i only want the statement printed if it is inside the include statement. besides it not neccesary be a php file. it might be a html file.

the particular code would even print out even if the filename isnt inside the include function.
basically what i am trying to do here, is to parse the string and check for the included files.

thanks again
[/quote]


Please keep reading this till I get time to write the complete code for you.

[a href=\"http://in2.php.net/preg_match\" target=\"_blank\"]http://in2.php.net/preg_match[/a]

and

[a href=\"http://www.google.co.in/search?q=regular+expressions\" target=\"_blank\"]http://www.google.co.in/search?q=regular+expressions[/a]



#7 laavanya14

laavanya14
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 14 April 2006 - 11:50 AM

[!--quoteo(post=364751:date=Apr 14 2006, 05:59 AM:name=bonaparte)--][div class=\'quotetop\']QUOTE(bonaparte @ Apr 14 2006, 05:59 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Please keep reading this till I get time to write the complete code for you.

[a href=\"http://in2.php.net/preg_match\" target=\"_blank\"]http://in2.php.net/preg_match[/a]

and

[a href=\"http://www.google.co.in/search?q=regular+expressions\" target=\"_blank\"]http://www.google.co.in/search?q=regular+expressions[/a]
[/quote]


hi again..thank you very much again.

i would check it out.
i just dont understand how to set the conditions for the pattern thing inside the preg_match statement




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users