Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 4 123 ... LastLast
Results 1 to 15 of 47
  1. #1
    Regular Coder
    Join Date
    Jul 2009
    Posts
    187
    Thanks
    16
    Thanked 8 Times in 8 Posts

    Get in between two symbols

    I want to be able to get a value in between two symbols, can be any, for example:

    [blah] and it would loop through the document and find a place where something starts with "[" and ends with "]" (for verification purposes, so that things like "[" with no "]") and the returned information would be "blah", and the information would be storred in an array.

    I am looking to use this with file_get_contents, I get some of it, I just don't know how to search for something in this way efficiently or even at all xD

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    This is a perfect case for a regular expression.

    http://www.regular-expressions.info/php.html

    If you become overwhelmed, you are in the right place. regex is an advanced programming concept.

  • #3
    Regular Coder
    Join Date
    Jul 2009
    Posts
    187
    Thanks
    16
    Thanked 8 Times in 8 Posts
    thanks, I was thinking of a regex, just don't know how I would create one, so i'll have a look at the link you provided and get back to you ^_^

  • #4
    Regular Coder
    Join Date
    Jul 2009
    Posts
    187
    Thanks
    16
    Thanked 8 Times in 8 Posts
    Okay, I have figured out the regex, but I don't understand how I would use this to extract the information I need, I thought of an example..
    "{blah}" would be replaced with something in a file found at "root/site/something/blah.php" and not [blah.php] or something
    the regex I tested with is the following:
    PHP Code:
        $blah   = ("{BLAH} my name is Jazz!!!");
        
    $something preg_replace("%{(.*?)}%""FOUND"$something);
        echo(
    $something); 
    and it replaces "{BLAH}" with FOUND, but this isn't what I want as the thing which would need to be changed will be a dynamic value? (Which is between the {...} whilst still getting rid of the braces)
    Last edited by Jazz914; 10-24-2009 at 02:16 AM.

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Jazz914 View Post
    Okay, I have figured out the regex, but I don't understand how I would use this to extract the information I need, I thought of an example..
    "[blah]" would be replaced with something in a file found at "root/site/something/blah.php" and not [blah.php] or something
    the regex I tested with is the following:
    PHP Code:
        $blah   = ("{BLAH} my name is Jazz!!!");
        
    $something preg_replace("%{(.*?)}%""FOUND"$something);
        echo(
    $something); 
    and it replaces "{BLAH}" with FOUND, but this isn't what I want as the thing which would need to be changed will be a dynamic value?
    PHP Code:
    $blah "{BLAH} my name is Jazz!!!";
    $something preg_replace("/\{([^\}]+)\}/","$1",$something);
    echo 
    $something
    read both PCRE and Posix Regex from this link:

    http://www.php.net/manual/en/refs.basic.text.php

    there are also some examples for both.

    best regards

  • #6
    Regular Coder
    Join Date
    Jul 2009
    Posts
    187
    Thanks
    16
    Thanked 8 Times in 8 Posts
    Thanks thats one step closer to the thing I need, but I am trying to replace the thing within the braces with information from a file, for example this is how I would of thought it
    works..
    PHP Code:
    $something preg_replace("/\{([^\}]+)\}/"file_get_contents($1.".php"), $blah); 
    But it errors, so I tried this:
    PHP Code:
    $something preg_replace("/\{([^\}]+)\}/"file_get_contents("$1.php"), $blah); 
    But then it actually looks for a file named "$1"

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Jazz914 View Post
    Thanks thats one step closer to the thing I need, but I am trying to replace the thing within the braces with information from a file, for example this is how I would of thought it
    works..
    PHP Code:
    $something preg_replace("/\{([^\}]+)\}/"file_get_contents($1.".php"), $blah); 
    But it errors, so I tried this:
    PHP Code:
    $something preg_replace("/\{([^\}]+)\}/"file_get_contents("$1.php"), $blah); 
    But then it actually looks for a file named "$1"
    PHP Code:
    $filename preg_replace("/\{([^\}]+)\}/""$1"$blah);
    $filecontent file_get_contents($filename ".php");
    echo 
    "[" $filecontent "]";  // ??? 
    best regards

  • #8
    Regular Coder
    Join Date
    Jul 2009
    Posts
    187
    Thanks
    16
    Thanked 8 Times in 8 Posts
    For some reason the preg_replace is ignored :S
    This is what I get:

    Warning: file_get_contents(blah my name is jazz!!!.php) [function.file-get-contents]: failed to open stream: No such file or directory in G:\testing.php on line 4
    []

    when it should just be "blah.php"

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Jazz914 View Post
    For some reason the preg_replace is ignored :S
    This is what I get:

    Warning: file_get_contents(blah my name is jazz!!!.php) [function.file-get-contents: failed to open stream: No such file or directory in G:\testing.php on line 4
    []

    when it should just be "blah.php"
    wrong filename?, wrong place?

    best regards
    Last edited by oesxyl; 10-24-2009 at 04:03 PM.

  • #10
    Regular Coder
    Join Date
    Jul 2009
    Posts
    187
    Thanks
    16
    Thanked 8 Times in 8 Posts
    its the wrong file name because the preg_replace is being ignored...

    PHP Code:
    $blah "{blah} my name is Jazz!!!";
    $filename preg_replace("/\{([^\}]+)\}/""$1"$blah);
    $filecontent file_get_contents($filename ".php");
    echo 
    "[" $filecontent "]";  // ??? 
    the code above doesn't look for "blah.php" it looks for the whole quote, "blah my name is Jazz!!!.php"

  • #11
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Jazz914 View Post
    its the wrong file name because the preg_replace is being ignored...

    PHP Code:
    $blah "{blah} my name is Jazz!!!";
    $filename preg_replace("/\{([^\}]+)\}/""$1"$blah);
    $filecontent file_get_contents($filename ".php");
    echo 
    "[" $filecontent "]";  // ??? 
    the code above doesn't look for "blah.php" it looks for the whole quote, "blah my name is Jazz!!!.php"
    PHP Code:
    $blah "{blah} my name is Jazz!!!";
    $filename preg_replace("/\{([^\}]+)\}/""$1"$blah);
    print 
    $filename// <--- insert this and test
    $filecontent file_get_contents($filename ".php");
    echo 
    "[" $filecontent "]";  // ??? 
    best regards

  • #12
    Regular Coder
    Join Date
    Jul 2009
    Posts
    187
    Thanks
    16
    Thanked 8 Times in 8 Posts
    Sorry forgot to mention I tried that, the result was

    "blah my name is Jazz!!!" when it should just be "blah"

  • #13
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Jazz914 View Post
    Sorry forgot to mention I tried that, the result was

    "blah my name is Jazz!!!" when it should just be "blah"
    yes, is my fault, I didn't test it.

    PHP Code:
    <?php

    $blah 
    "{blah} my name is Jazz!!!";
    $filename preg_replace("/\{([^\}]+)\}.*/""\\1"$blah);
    print 
    $filename// <--- insert this and test
    if(file_exists($filename)){
      
    $filecontent file_get_contents($filename ".php");
      echo 
    "[" $filecontent "]";  // ???  
    }
    ?>
    adding a .* after the match part will remove the rest of the text.

    best regards
    Last edited by oesxyl; 10-25-2009 at 10:22 AM.

  • Users who have thanked oesxyl for this post:

    Jazz914 (10-25-2009)

  • #14
    Regular Coder
    Join Date
    Jul 2009
    Posts
    187
    Thanks
    16
    Thanked 8 Times in 8 Posts
    I tried it and the format of the word is correct but for some reason it won't find the file
    I added this to the end of the code for debugging purposes (as it actually states something)
    PHP Code:
    }else{
        echo(
    "File doesn't exist");    

    but echoing out the quote it echos out the word "blah" and not the whole statement which is good but confusing as when you add the .php to it, it is "blah.php" which is correct :S
    ------------------------------------
    EDIT:
    Just noticed that you have the file check before actually adding the .php so its actually just looking for the file "blah" instead of "blah.php" :P Works now, thanks ^^

    One more thing, if I wanted multiple of these would I loop through it using a for loop?

    By multiple I mean, having something like this: "{Blah}, {Blah2}, {Blah3}" and is it possible to not erase the rest of the quote so it turns out like this:

    {FirstFileContent} content in main file {SecondFileContent} More content in main file {ThirdFileContent} and on...
    Last edited by Jazz914; 10-25-2009 at 03:27 PM.

  • #15
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Jazz914 View Post
    I tried it and the format of the word is correct but for some reason it won't find the file
    I added this to the end of the code for debugging purposes (as it actually states something)
    PHP Code:
    }else{
        echo(
    "File doesn't exist");    

    but echoing out the quote it echos out the word "blah" and not the whole statement which is good but confusing as when you add the .php to it, it is "blah.php" which is correct :S
    ------------------------------------
    EDIT:
    Just noticed that you have the file check before actually adding the .php so its actually just looking for the file "blah" instead of "blah.php" :P Works now, thanks ^^
    the reason I put there was to change output in case of error( not implemented but that was the intention).

    One more thing, if I wanted multiple of these would I loop through it using a for loop?
    it is possible but you also can use preg_match or preg_filter instead of preg_replace.

    By multiple I mean, having something like this: "{Blah}, {Blah2}, {Blah3}" and is it possible to not erase the rest of the quote so it turns out like this:

    {FirstFileContent} content in main file {SecondFileContent} More content in main file {ThirdFileContent} and on...
    a note about preg_replace. If you have:
    PHP Code:
    $a preg_replace($regex$prelace$b); 
    after this line $b will be same as before.
    Back to your problem, I would use preg_match and extract a array with all filenames, then check in a loop if each exists and do whatever you want to do with content.
    Take care to filenames and path, are different between windows and linux, and try to use absolute path, more exactly relative to the root of the website( if is a site where you use this).

    best regards


  •  
    Page 1 of 4 123 ... LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •