Web Analytics Made Easy -
StatCounter Does this need regex to remove data from a list? - CodingForum

Announcement

Collapse
No announcement yet.

Does this need regex to remove data from a list?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Does this need regex to remove data from a list?

    Hi,

    I have this text file with a list of data like this:

    "rollerblade",26,
    "skates rollerblade",15,
    "rollerblade inline",12,
    "rollerblade inline skates",11,

    But I just want the first phrase without the quotes
    like this:

    rollerblade
    skates rollerblade
    rollerblade inline
    rollerblade inline skates


    What is the simplest way to do this in php ?
    Is regex what I need ?

    I guess I need to find the ", and delete from after that ?


    How would I do that ?

    Thanks.
    David.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    See either - PHP: fgetcsv - Manual or PHP: str_getcsv - Manual
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

    Comment


    • #3
      So ...

      I will start by putting them into an array ...

      $all_prodkeys = file_get_contents($prodkeys);
      $prodkeys_arr = explode("\n", $all_prodkeys);


      foreach ($prodkeys_arr as $key => $value) {
      $prodkeys_arr [$key] = ???? ($value);
      }

      SO .. its that bit with the ??? that I think needs a regex ?
      If you want to attract and keep more clients, then offer great customer support.

      Support-Focus.com. automates the process and gives you a trust seal to place on your website.
      I recommend that you at least take the 30 day free trial.

      Comment


      • #4
        I think this should do it without using regex.

        $thekeys = array();
        $outfile = 'prod_keys';
        $all_prodkeys = file_get_contents($prodkeys);
        $prodkeys_arr = explode(PHP_EOL, $all_prodkeys);
        foreach (prodkeys_arr as $value) {
        $new_value = str_replace('"','',$value);
        $pos = strpos($new_value, ',');
        $new_value = substr($new_value,0,($pos-1));
        $thekeys[] = $new_value;
        }

        $thekeys = implode(PHP_EOL,$thekeys);
        file_put_contents($outfile, $thekeys);
        If you want to attract and keep more clients, then offer great customer support.

        Support-Focus.com. automates the process and gives you a trust seal to place on your website.
        I recommend that you at least take the 30 day free trial.

        Comment


        • #5
          Originally posted by jeddi View Post
          I think this should do it without using regex.

          $thekeys = array();
          $outfile = 'prod_keys';
          $all_prodkeys = file_get_contents($prodkeys);
          $prodkeys_arr = explode(PHP_EOL, $all_prodkeys);
          foreach (prodkeys_arr as $value) {
          $new_value = str_replace('"','',$value);
          $pos = strpos($new_value, ',');
          $new_value = substr($new_value,0,($pos-1));
          $thekeys[] = $new_value;
          }

          $thekeys = implode(PHP_EOL,$thekeys);
          file_put_contents($outfile, $thekeys);
          Given that you're working with a single large string, I think it's easiest and possibly more efficient to do this with a regular expression backreference rather than converting the string into an array and iterating over it. This code works for me:

          Code:
          <?php
          	$product_keys = file_get_contents('text/product.keys.1.txt');
          	$product_keys = preg_replace('/"([^"]+)"/', '$1', $product_keys);
          ?>
          <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-Latn-US-u-em-emoji">
          	<head>
          		<title>Demo Page</title>
          	</head>
          	<body>
          		<p>This is a demo page. Output:</p>
          		<pre><output><?= $product_keys; ?></output></pre>
          	</body>
          </html>

          Comment


          • #6
            Looks Good,
            Thanks
            If you want to attract and keep more clients, then offer great customer support.

            Support-Focus.com. automates the process and gives you a trust seal to place on your website.
            I recommend that you at least take the 30 day free trial.

            Comment

            Working...
            X