Click to See Complete Forum and Search --> : read string between two underscore
mani-X
04-08-2008, 06:42 PM
Hello,
I need a function to get the string between _ and _ in the following example.
$file="mon_iam_miammiami_hellohello.doc";
i would like to have the characters "miammiami" inside a variable.
i have done it so far with this script:
$char=substr($file,8,-15);
echo $char;// miammiami
my problem is that the script is not flexible, if the beginning or any part of the file is changed it will fail.
example: mo_iam_miammiami_hellohell.doc
it will print "iammiam" which will not be the string i want to recuperate.
scragar
04-08-2008, 06:51 PM
if(preg_match_all("/_(.*?)_/", $file, $matches);
print_r($matchs[1]);
NogDog
04-09-2008, 04:21 AM
One question you need to resolve is, will it always be "aaa_bbb_ccc_ddd.doc", where the value of interest will be "ccc"? Or could it sometimes be "aaa_ccc_ddd.doc" or "aaa_bbb_ccc_ddd_eee.doc", etc.? In other words, how do you know it's the "_maimmiami_" pattern that you want to grab in your sample instead of the "_iam_" pattern?
mani-X
04-09-2008, 11:38 AM
i have managed this, even if i have changed the any value outside the two underscores:
$value = preg_replace("/.*\_(.*)\_.*/", "$1", $file, 1);
echo $value; //miammiami
one last thing how you think i can get the value between the underscore and the period in the file i.e.: _hellohello.doc
i want 2 variables to hold the first 5 characters and another to hold the other 5 characters:
example:
$var1 to hold hello
$var2 to hold hello
Nogdog be advised the file will always in format "aaa_bbb_ccc_ddd". but i need to make sure that if it changes one day i can get the desired part of it.
Scragar what do you think?? I will try your code.
scragar
04-09-2008, 11:44 AM
well mine just stored an array of values between underscores in $matches[1] the easy way :P
$preDot = preg_replace("/_([^_]*)\./", "$1", $file);will store value between an underscore and the period in $preDot
mani-X
04-09-2008, 12:01 PM
i have tried your code:
$preDot = preg_replace("/_([^_]*)\./", "$1", $file);
it prompts me: syntax error unexpected $
I will look deeper in the preg_replace functions
thnx for your help Scagar
scragar
04-09-2008, 12:04 PM
I wrote it wrong:
preg_match_all("/_(.*?)_/", $file, $matches);
print_r($matches[1]);
it's possible to put it in an if code if you wanted, personaly I would use that as an extra layer of protection against bad results.
mani-X
04-09-2008, 12:09 PM
still the same problem. the same error, all i need to understand more is the regex(regularexpressions)
Huevoos
04-09-2008, 01:14 PM
The regex is fine something else must be wrong in your script
kurby
04-09-2008, 01:39 PM
For those of us that are regex illiterate, explode will do the same thing.
$subStrs = explode("_", $file);
mani-X
04-10-2008, 09:35 AM
hello thanks a lot,
i have done it this way:
$file="mon_iam_miammiami_hellohello.doc";
$predot = preg_replace("/.*_([^_]*)\.doc/", "$1", $file,1);
echo $predot; //hellohello
very great help from you all, Scragar thnx a lot, Nogdog,huevoos, Kurby great support. I will look further to overcome this.