SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face Help needed to "find" a certain string in file using regex...

    Hello,

    I have a file with contents like this:
    text here
    asdfasdf
    asdf
    f
    asdfsdf
    <!-- #startpart -->
    stuff goes here
    <!-- #endpart -->
    adsf
    adsf
    ffd
    ff
    df
    I am trying to write some php code that will allow me to check and see if the file contains the a valid pair of "startpart" and "endpart" comments. (There can be an unknown number of spaces between each part of the HTML comments. The "#startpart" comment might also have some other junk in it like this: "<!-- #startpart anything else here -->". However, only the "<!-- #startpart -->" portion is actually required and should be checked if it exists.)

    Does anyone know how to do this?

    Here's what I have so far:
    PHP Code:
    $lines file($file);
    $count 0;
    for(
    $x=0$x<count($lines); $x++){ // For each line in the file..
    /* The \b in the pattern indicates a word boundary, so only the distinct
         word is matched, and not a word partial.
         The "i" after the pattern delimiter indicates a case-insensitive search
        */
    if (preg_match("/\b<!--\b(.*)\b#Startpart\b(.*)\b-->\b/i"$lines[$x])) { 
        
    $count += 1;
         }
    if (
    preg_match("/\b<!--\b(.*)\b#EndPart\b(.*)\b-->\b/i"$lines[$x])) { 
         
    $count += 1;
         }
    }
    if(!(
    $count%2) && $count>0) { 
    return 
    true// At least one *pair* of valid start/end tags was found.

    else { 
    return 
    false// No valid tags were found, or tags were not in pairs.

    However, this doesn't work. I'm not an expert with regex, so I'm hoping someone can help?

  2. #2
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this
    PHP Code:
     $lines file($file);
    $count 0;
    for(
    $x=0$x<count($lines); $x++){ // For each line in the file..
    /* The \b in the pattern indicates a word boundary, so only the distinct
         word is matched, and not a word partial.
         The "i" after the pattern delimiter indicates a case-insensitive search
        */
    if (preg_match("/<!-- *#startpart *-->/i"$lines[$x])) {
        
    $count += 1;
         }
    if (
    preg_match("/<!-- *#endpart *-->/i"$lines[$x])) {
         
    $count += 1;
         }
    }
    if(!(
    $count%2) && $count>0) {
    return 
    true// At least one *pair* of valid start/end tags was found.
    }
    else {
    return 
    false// No valid tags were found, or tags were not in pairs.


  3. #3
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jaswinder_rana
    try this
    Yes, that worked. Thanks!

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    Erial, NJ
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Beating a dead horse, really bad here, but Id rather not start a new thread on it.

    How can you return 'stuff goes here' portion?

  5. #5
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try
    HTML Code:
    <!-- #startpart -->
    stuff goes here
    <!-- #endpart -->
    Taking the above html cod ein consideration, try this
    PHP Code:
    $line '<!-- #startpart -->'.
          
    'stuff goes here'.
          
    '<!-- #endpart -->';//ASSUMING you would open the file and get the text from there or read the html page.

    if (preg_match("~<!--\s*#startpart\s*-->(.*?)<!--\s*#endpart\s*-->~i"$line,$matchArr))
    {
       
    print_r($matchArr);

    Logic is correct, might be syntax problem somewhere, not tested.
    ---------------------------
    Errors = Improved Programming.
    My Site


Bookmarks

Posting Permissions

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