ballouta Posted June 20, 2008 Share Posted June 20, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/ Share on other sites More sharing options...
rhodesa Posted June 20, 2008 Share Posted June 20, 2008 if(preg_match('/[^ ](.+?\.ask)/',$text,$matches)){ $filename = $matches[1]; echo $filename; } Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/#findComment-570586 Share on other sites More sharing options...
Stephen Posted June 20, 2008 Share Posted June 20, 2008 If you mean searching for a file name from 0 going up, then try this: <?php $i=0; do { if (file_exists($i.".ask")) { echo("Found file: ".$i.".ask"); } $i+=1; } while (!file_exists($i.".ask")); ?> Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/#findComment-570588 Share on other sites More sharing options...
ballouta Posted June 20, 2008 Author Share Posted June 20, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/#findComment-570593 Share on other sites More sharing options...
Stephen Posted June 20, 2008 Share Posted June 20, 2008 Mine looks for a file from 0.ask on until it finds one. Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/#findComment-570595 Share on other sites More sharing options...
ballouta Posted June 20, 2008 Author Share Posted June 20, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/#findComment-570596 Share on other sites More sharing options...
jonsjava Posted June 20, 2008 Share Posted June 20, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/#findComment-570601 Share on other sites More sharing options...
ballouta Posted June 20, 2008 Author Share Posted June 20, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/#findComment-570607 Share on other sites More sharing options...
jonsjava Posted June 20, 2008 Share Posted June 20, 2008 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/#findComment-570609 Share on other sites More sharing options...
ballouta Posted June 20, 2008 Author Share Posted June 20, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/#findComment-570614 Share on other sites More sharing options...
jonsjava Posted June 20, 2008 Share Posted June 20, 2008 <?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 = str_replace("'", "", $value); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/#findComment-570623 Share on other sites More sharing options...
rhodesa Posted June 21, 2008 Share Posted June 21, 2008 sorry...had it a little off: if(preg_match('/([^ ]\.ask)/',$text,$matches)){ $filename = $matches[1]; echo $filename; } Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/#findComment-570771 Share on other sites More sharing options...
ballouta Posted June 21, 2008 Author Share Posted June 21, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/#findComment-570829 Share on other sites More sharing options...
rhodesa Posted June 22, 2008 Share Posted June 22, 2008 if(preg_match('/\'(.+?\.ask)\'/',$text,$matches)){ $filename = $matches[1]; echo $filename; } Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/#findComment-571312 Share on other sites More sharing options...
ballouta Posted June 22, 2008 Author Share Posted June 22, 2008 Great Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/#findComment-571345 Share on other sites More sharing options...
ballouta Posted June 23, 2008 Author Share Posted June 23, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/#findComment-572255 Share on other sites More sharing options...
effigy Posted June 23, 2008 Share Posted June 23, 2008 <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> Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/#findComment-572290 Share on other sites More sharing options...
ballouta Posted June 23, 2008 Author Share Posted June 23, 2008 Thanks the code is perfect, the output is: Array ( [0] => Array ( [0] => 358952017459231.ASK.ASK [1] => 358952017459231.ask ) ) But i need to use this varaiable that stores the file name, How? thanks Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/#findComment-572383 Share on other sites More sharing options...
effigy Posted June 23, 2008 Share Posted June 23, 2008 The array structure shows you: $matches[0][0]. Actually, since there is only going to be one match, use preg_match and $matches[0]. Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/#findComment-572396 Share on other sites More sharing options...
ballouta Posted June 23, 2008 Author Share Posted June 23, 2008 Great you are Genius. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/#findComment-572406 Share on other sites More sharing options...
ballouta Posted July 2, 2008 Author Share Posted July 2, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/#findComment-579913 Share on other sites More sharing options...
ballouta Posted July 2, 2008 Author Share Posted July 2, 2008 it is working now thank you Quote Link to comment https://forums.phpfreaks.com/topic/111172-solved-searching-string/#findComment-579923 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.