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.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    preg_matching & overwriting

    I'm trying to put together a script that will do the following:

    1. Open and parse a csv file

    2. Match a specific value within a constant tag on every row

    3. Strip away the rest of the string

    4. Save and close the file

    For example, these might be relevant 3 rows in the original file.csv:

    Code:
    <tag>My name is John</tag>
    <tag>They call him John</tag>
    <tag>John is 15 years old</tag>
    And here's what I'd like in the overwritten file.csv:

    Code:
    John
    John
    John
    Non-matches should be overwritten with nothing i.e. blanked.

    Here is the basic code I'm trying to flesh out, I know I should probably be using fgetcsv somewhere and proper regex but that's why I'm here:

    PHP Code:
    <?php
    if($fh fopen('file.csv''r+')){
    $string fread ($fhfilesize('file.csv'));
    preg_match_all("/^(<tag>)(?=.*?John)(?=.*?</tag>)$/"$string$match);
    foreach(
    $match[0] as $value)
    $string str_replace($string$value$string);

    // then strip the rest..........

    fclose($fh);
    } else {
    die (
    "Error");
    }

    echo 
    $result;
    ?>
    I think the general concept is clear, any ideas how to get it working?

    Thanks

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    If you don't need the rest of the content, why not just implode the $matches into your file:
    PHP Code:
    $string fread ($fhfilesize('file.csv'));
    preg_match_all("/^(<tag>)(?=.*?John)(?=.*?</tag>)$/"$string$match);
    file_put_contents('file.csv'implode("\r\n"$match[1])); 
    Not tested, just an example.

  • #3
    New Coder
    Join Date
    Jul 2008
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I just tried that, it echos all rows but wipes the csv file entirely.

    I think the write function and regex aren't quite right here, it probably also needs an array or something to match each specific row.

    This regex seems to work a bit better but I'm still not sure how to extract only "John" out of the string/s:

    PHP Code:
    preg_match_all('/<tag\b[^>]*>(.*?John)<\/tag>/'$str$matches); 

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    preg_match_all('#<tag.+?(John).*?</tag>#i', $str, $matches);

  • Users who have thanked MattF for this post:

    Herz0g (02-23-2010)

  • #5
    New Coder
    Join Date
    Jul 2008
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hey, that actually worked, thanks!

    Just a couple more things: when no match for "John" is found it simply deletes the entire cell, which messes up the wider structure of the file. Could something else like "Data Not Found" be substituted instead, as in an if-else statement.

    Also, the overwrite deletes all other columns, can this be avoided? I've tried using FILE_APPEND but no luck so far.

    Oh, and how would ucwords be applied here to capitalize only the first letter of every word e.g. "JOHN SMITH/john smith" -> "John Smith"?

    Thanks again.


  •  

    Posting Permissions

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