Web Analytics Made Easy -
StatCounter Removing characters from a string - CodingForum

Announcement

Collapse
No announcement yet.

Removing characters from a string

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

  • Removing characters from a string

    Removing characters from a string

    Hi there,

    I have came up with the following code ..
    PHP Code:
    $open                     strpos($data"<DATA>") + 6;
    $close                 strpos($data"</DATA>");
    $result                 substr($data$open$close $open); 
    This grabs everything between <DATA> and </DATA> and puts it into the $result variable. Leaving $data intact, however I wish to remove the value of $result from $data entirely, so that instead of just returning whats inbetween <DATA></DATA> it actually removes or 'deletes' what is between it...

    Can this be done?

    Any thoughts apperciated

    cheers
    sir p
    Reseller Web Hosting - Unlimited websites from $5.49
    cakephp tutorials || Buying or sell websites? Do it here, free! || Professional Website & PHP development

    Freelance Magento Job Classifieds

  • #2
    PHP Code:
    $open strpos($data"<DATA>") + 6
    $close strpos($data"</DATA>"); 
    $result substr($data$open$close $open);  
    $data substr($data0$open) . substr($data$close); 
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

    Comment


    • #3
      PHP Code:
      <?php
      $data
      'somethin <DATA>data in tag</DATA> something else';
      $open strpos($data"<DATA>") + 6
      $close strpos($data"</DATA>"); 
      $result substr($data$open$close $open);
      $data substr_replace($data''$open$close $open);
      ?>
      I'm not sure if this was any help, but I hope it didn't make you stupider.

      Experience is something you get just after you really need it.
      PHP Installation Guide Feedback welcome.

      Comment


      • #4
        PHP Code:
        $result preg_replace("~(<DATA>)(.*)(</DATA>)~i""$1$3"$string); 
        will replace <DATA> wahtever in here </DATA> with <DATA></DATA>

        Use
        PHP Code:
        $result preg_replace("~(<DATA>)(.*)(</DATA>)~i"""$string); 
        to get rid of the whole <DATA> whatever </DATA> block, including the <DATA> tags
        Last edited by NancyJ; Oct 4, 2006, 09:51 AM.
        http://www.hazelryan.co.uk

        Comment


        • #5
          3 sollution, so which one to use...

          i never used substr_replace since it's supposed to be very slow in php versions prior to 4.3.5. (preg_replace() shold certainly be slower)

          so i made a small testscript where $data contains about 1000 lines of data (from a logfile) with the <DATA>foobar</DATA> in the middle.
          i then ran the 3 commands that remove all content between the tags (10 000 iterations for each command) on a PHP 5.1.2 environment.

          marek_mar his command is apparently fastest --> about 0.13 seconds for 10000 iterations.
          my code takes about 0.7 seconds, and NancyJ her code takes about 1.1 second.

          so in my opinion, marek_mar has the best sollution. in any case, for PHP > 4.3.5 ...
          Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

          Comment

          Working...
          X