Jump to content

Find particular word in a string


laavanya14

Recommended Posts

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.
Link to comment
Share on other sites

[!--quoteo(post=364707:date=Apr 14 2006, 12:56 AM:name=Laavanya)--][div class=\'quotetop\']QUOTE(Laavanya @ Apr 14 2006, 12:56 AM) [snapback]364707[/snapback][/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.
Link to comment
Share on other sites

Helo thanks for the reply.


Assuming i have a
[code]$mystring = "this is a include('test.php'). I would like to test include('pooh.php'); " [/code]

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
Link to comment
Share on other sites

[!--quoteo(post=364714:date=Apr 14 2006, 01:43 AM:name=bonaparte)--][div class=\'quotetop\']QUOTE(bonaparte @ Apr 14 2006, 01:43 AM) [snapback]364714[/snapback][/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) [snapback]364746[/snapback][/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);
Link to comment
Share on other sites

[!--quoteo(post=364746:date=Apr 14 2006, 05:46 AM:name=bonaparte)--][div class=\'quotetop\']QUOTE(bonaparte @ Apr 14 2006, 05:46 AM) [snapback]364746[/snapback][/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
Link to comment
Share on other sites

[!--quoteo(post=364749:date=Apr 14 2006, 05:53 AM:name=Laavanya)--][div class=\'quotetop\']QUOTE(Laavanya @ Apr 14 2006, 05:53 AM) [snapback]364749[/snapback][/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]

Link to comment
Share on other sites

[!--quoteo(post=364751:date=Apr 14 2006, 05:59 AM:name=bonaparte)--][div class=\'quotetop\']QUOTE(bonaparte @ Apr 14 2006, 05:59 AM) [snapback]364751[/snapback][/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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.