Need help with preg_replace

Discussion in 'PHP' started by HungryMinds, Aug 27, 2011.

  1. #1
    Hi!

    Please refine this line using preg_replace()

    
    $query = "id, pic=>img, detail=>a, dt=>h, type, active";
    
    Code (markup):
    I want to remove "=>img" and "=>a" and "=>h"

    And the result should be like this:

    
    $query = "id, pic, detail, dt, type, active";
    
    Code (markup):
    Please Guide.
     
    Solved! View solution.
    HungryMinds, Aug 27, 2011 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    It doesn't make sense why you would do that ?

    Basically all you have to do is write the new line under the first one... the last line will take presidency.
     
    Last edited: Aug 27, 2011
    MyVodaFone, Aug 27, 2011 IP
  3. #3
    $query = "id, pic=>img, detail=>a, dt=>h, type, active";
    $query = preg_replace("@=>[a-z]*@","",$query);
    echo $query;
    PHP:
     
    Technoslab, Aug 27, 2011 IP
    HungryMinds likes this.
  4. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #4
    Thanks for reply friends.

    Thanks a lot @Technoslab

    Exactly this i was searching.

    Could you please tell me that what "@" is doing here?

    Now i've another query. Please set pattern for this too.

    
    $query = "id=>ID, pic=>Picture/img, detail=>Detail/a, dt=>Date/h, type=>Type, active=>Active";
    
    Code (markup):
    I want 3 patterns.

    1) id, pic, detail, dt, type, active
    2) ID, Picture, Dtail, Date, Type, Active
    3) '', img, a, h, '', ''

    Please guide like your's previous one.
     
    Last edited: Aug 27, 2011
    HungryMinds, Aug 27, 2011 IP
  5. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #5
    I just want to left "Picture" in it using preg_replace();

    
    icon=>Picture/img
    
    Code (markup):
    I tried:

    
    preg_replace("/[a-zA-Z_]*=>/", "", 'icon=>Picture/img');
    
    Code (markup):
    It replaced 'icon=>'

    "/img" will be optional.

    Please guide.
     
    HungryMinds, Aug 28, 2011 IP
  6. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #6
    Hi,

    I've this query

    
    select id=>ID, name=>Name/h, icon=>My Picture/img, link=>Link/a, active=>Action from es_socialmedia where del='no' order by id desc limit 0,20
    
    Code (markup):
    I just want to remove these:

    =>ID
    =>Name/h
    =>icon=>My Picture/img
    =>Link/a
    =>Action

    I'm using this preg_replace()

    
    preg_replace("@=>[a-zA-Z_(\/(.?))]*@", "", $sql);
    
    Code (markup):
    It's working perfect. But! when i'm using space between "My Picture/img"
    Then i'm getting error.
    I tried (\s) too, But it's not working :S
    If i'm using "My_Picture/img" with underscore. It works perfect.
    Plz could anyone refine this sql query for me?
    Help will be appreciated.
     
    HungryMinds, Sep 9, 2011 IP