Jump to content

[SOLVED] Searching String


ballouta

Recommended Posts

Hi

 

Is it posible to look for a file name in a sentence like this:

 

Your file '357968007770859.ask' has been calculated.

 

The extension is always .ask while the name changes of course BUT it is important for me to get the file name.

 

thank You

 

 

Link to comment
Share on other sites

rhodesa:

 

$text = "Your file '357968007770859.ask' has been calculated.";

$matches = "ask";

 

if(preg_match('/[^ ](.+?\.ask)/',$text,$matches)){

  $filename = $matches[1];

  echo $filename;

}

 

Output: our file '357968007770859.ask

 

Text in color shouldn't appear. I like it, Would u please fix it

 

stephan:

sorry, i am very biginner, I didn't understand where the text that will look in?

would u please explain more.

 

thanks again

Link to comment
Share on other sites

actually i am not sure if it is always begings with Zero.

I think you mean the code finds numbers between 0-9 (which are the file name) but didn't know where's the text variable that going to look for.

 

the preg_match is easier for me if fixed. please

 

 

Link to comment
Share on other sites

would something like this work:

<?php
$data = "this is an example of searching for 357968007770859.ask";
$data_array = explode(" ", $data);
foreach ($data_array as $value){
if (strstr($value, ".ask")){
	$result[] = $value;
}
}
print_r($result);
?>

this will give you any instances of *.ask, in array form (so if you have more than one per search, it will pull up all)

I would use preg_match, but I suck at it.

Link to comment
Share on other sites

Hi copied ur code as is, this was the output:

Array ( [0] => 357968007770859.ask )

 

it is not exactly as i need it, i need to save this file name to make a check for it in a DB table.

please note that i will always have only ONE name in this sentence.

Could you please modfiy the code.

thanks for your patience

Link to comment
Share on other sites

<?php
$data = "this is an example of searching for 357968007770859.ask";
$data_array = explode(" ", $data);
foreach ($data_array as $value){
if (strstr($value, ".ask")){
	$result = $value;
}
}
?>

your data that you need is saved in the variable $result. run your query using that variable.

Link to comment
Share on other sites

Your Code works 100% correctly. still small issue

 

the text that will be searched is always in this format:

 

Your file '357968007770859.ask' has been calculated.

 

Notice that single quotation before and after the file name, when I used this sentnece in your code the output file name was:

'357968007770859.ask'

I need to remove the single quotations from begining and the end of the file name, please.

thanks

Link to comment
Share on other sites

Hi

 

$text = "Your file '357968007770859.ask' has been calculated.";

$matches = "ask";

 

Actaully I prefer your short codes but your last preg_match gave me this output:

 

9.ask

whiel ti should be: 357968007770859.ask without any single quotation on both sides

 

 

thanks again

Link to comment
Share on other sites

Hello

 

I always have one of the following sentences format.

 

Error processing your order for ASK file: 358952017459231.ASK.ask!

OR

Error processing your order for ASK file: 358952017459231.ask!

 

I need to store the .ask name in a variable. there's always an ! mark at the end!

 

e.g: First String    = 358952017459231.ASK.ask

      Second String =  358952017459231.ask

 

If an error occured i will always receive one of the above error messages, but the user in the first one include .ASK in the file name as you know.

 

Kindly help and thanks in advance.

Link to comment
Share on other sites

<pre>
<?php
$data = <<<DATA
Error processing your order for ASK file: 358952017459231.ASK.ask!
OR
Error processing your order for ASK file: 358952017459231.ask!
DATA;
preg_match_all('/\S+\.ask(?=!)/i', $data, $matches);
print_r($matches);
?>
</pre>

Link to comment
Share on other sites

  • 2 weeks later...

Hello

 

i am using now this preg_match and it working perfect. It is always applied on this sentence:

$subject = Your file '357968007770859.ask' has been calculated

 

<?php
preg_match('/\S+\.ask(?=!)/i', $subject, $matches);
  	$filename = $matches[0];
?>

 

I just noticed that the above $sbject might be:

Your file '_357968007770859.ask' has been calculated.

the code couldn't catch the file name with an underscore in it

 

Would you please give me another preg to look for the ask file name if it begins with _ ?

 

Thank you

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.