Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    What is the best function to replase these strings?


    Hi
    i have a string such as this :
    Code:
    @default:break;}}@
    is there any php function to replace the first red one to "#" and the last red one to "/#"
    ?
    or i have to write an algorithm?

    Regards
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,850
    Rep Power
    6351
    Are they all that one string? Or do you mean for the black text to be variable?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0
    what is @ for? in php, if you want to replace a string use str_replace()

    Syntax:

    str_replace(find,replace,string,count)
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0
    Are they all that one string? Or do you mean for the black text to be variable?
    not actually !
    i have numbers of string such as these :
    Code:
    @some text here@
    !some text here!
    # some text here #
    etc.
    and want to replace first red one and last red one to other character.(html tags)

    what is @ for? in php, if you want to replace a string use str_replace()
    i see,but it can't do it.it is a special form of replace.
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,850
    Rep Power
    6351
    So now you've changed your bounding characters as well?

    What is the actual problem? You have strings WHERE, bounded by WHAT, and containing WHAT?

    Are these strings in a database, or just randomly in a big file?

    What are the bounding characters? Just #? #, @, !? Any special character?

    What can be in the middle of these strings? Whitespace? Other special characters? Other bounded blocks?

    What do you want them to look like?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  10. #6
  11. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,984
    Rep Power
    9397
    Replace the red characters? If you copy/pasted your example then you're using BBCode.
    PHP Code:
    $newstring preg_replace('/^[color=red].[/color](.*)[color=red].[/color]/i''#$1/#'$oldstring); 
    Not sure if you want to keep the red coloring though.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by ManiacDan
    So now you've changed your bounding characters as well?
    What is the actual problem? You have strings WHERE, bounded by WHAT, and containing WHAT?
    I don't know there is any function in php do that
    Originally Posted by ManiacDan
    Are these strings in a database, or just randomly in a big file?
    No,this is a text file.not in database
    Originally Posted by ManiacDan
    What are the bounding characters? Just #? #, @, !? Any special character?
    no just these and + some character such as '*' , 'bq.' etc.
    Originally Posted by ManiacDan
    What can be in the middle of these strings? Whitespace? Other special characters? Other bounded blocks?
    any change.for example. i want to replace "!" with =>> '<img src="' plus follow text and last "!" replace with =>> ' "/> '
    Originally Posted by ManiacDan
    What do you want them to look like?
    these are murkup editor codes,i want to convert it to tinyemce. thats it
    and thanks for your reply
  14. #8
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,850
    Rep Power
    6351
    What happens when your content contains multiple email addresses or multiple questions?

    Look at this post right here, do you see the problem?

    There's two question marks. Do you really want to remove them both and make my second sentence red or whatever?

    If you're trying to make a markup parser, you'll have to do it by hand. They are very, very complicated. You have to step through the string one character at a time looking for your markup, keeping track of your depth in the string and which tags you've already found. Since your "tags" are random, and variable length, you have to also keep a list of all possible tags, and make sure that you match closing tags case-insensitively.

    Look into how to write a markup parser, or check out the source code of the bbcode parsers.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by ManiacDan
    What happens when your content contains multiple email addresses or multiple questions?

    Look at this post right here, do you see the problem?

    There's two question marks. Do you really want to remove them both and make my second sentence red or whatever?

    If you're trying to make a markup parser, you'll have to do it by hand. They are very, very complicated. You have to step through the string one character at a time looking for your markup, keeping track of your depth in the string and which tags you've already found. Since your "tags" are random, and variable length, you have to also keep a list of all possible tags, and make sure that you match closing tags case-insensitively.

    Look into how to write a markup parser, or check out the source code of the bbcode parsers.
    thanks
    in my point of view , i have to write my own functions

    regards
  18. #10
  19. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,889
    Rep Power
    6444
    Use a regex and bound to '^' and '$' for the start and end of the string

    or use substr_replace to replace the first and last symbols in the string

    or use substr to pull all but the first and last characters of the string ...
    --Ax
    without exception, there is no rule ...
    Handmade Irish Jewellery
    Targeted Advertising Cookie Optout (TACO) extension for Firefox
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


    09 F9 11 02
    9D 74 E3 5B
    D8 41 56 C5
    63 56 88 C0
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    -- Jamie Zawinski
    Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
    BIT COINS ANYONE
  20. #11
  21. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,850
    Rep Power
    6351
    These strings exist mixed throughout a big text file. It's hard to tell because he absolutely refuses to show you some actual data, instead he keeps listing fake examples.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by Axweildr
    Use a regex and bound to '^' and '$' for the start and end of the string

    or use substr_replace to replace the first and last symbols in the string

    or use substr to pull all but the first and last characters of the string ...
    oh great !
    is there any exact example?
    thanks for your reply.
  24. #13
  25. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,889
    Rep Power
    6444
    You can look these functions up on http://php.net, or you could help us to help u by providing some live data

    The initial sample you gave is some badly formed php code, because of the multiple closing braces on the same line, if we deliver what it is you initially requested there's a very good chance your entire codebase will be rendered less than useless, and if you're doing that in tinymce then you have bigger issues than that.

    default: break; <-- pointless
    --Ax
    without exception, there is no rule ...
    Handmade Irish Jewellery
    Targeted Advertising Cookie Optout (TACO) extension for Firefox
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


    09 F9 11 02
    9D 74 E3 5B
    D8 41 56 C5
    63 56 88 C0
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    -- Jamie Zawinski
    Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
    BIT COINS ANYONE
  26. #14
  27. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,889
    Rep Power
    6444

    Sample of substr_replace


    php Code:
      $str='@ sentence @';
      echo $str."<br />";
      $newstr = substr_replace($str, '', strlen($str)-1, 1);
      $newstr = substr_replace($newstr, '', 0, 1);
      echo $newstr."<br />\n";

    Use with extreme caution, and back up your data first ...
    --Ax
    without exception, there is no rule ...
    Handmade Irish Jewellery
    Targeted Advertising Cookie Optout (TACO) extension for Firefox
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


    09 F9 11 02
    9D 74 E3 5B
    D8 41 56 C5
    63 56 88 C0
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    -- Jamie Zawinski
    Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
    BIT COINS ANYONE
  28. #15
  29. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,850
    Rep Power
    6351
    Note that everything Axweildr is saying assumes you have these strings just as you've presented, them. That is, "@hello@" is by itself in its own file or database field with no other content around it. You have made it clear that's not how you have your data, so this will not work for you.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo